Module: Hammock::ActiveRecordPatches::ClassMethods

Defined in:
lib/hammock/monkey_patches/active_record.rb

Instance Method Summary collapse

Instance Method Details

#base_modelObject



54
55
56
# File 'lib/hammock/monkey_patches/active_record.rb', line 54

def base_model
  base_class.to_s.underscore
end

#descriptionObject



58
59
60
# File 'lib/hammock/monkey_patches/active_record.rb', line 58

def description
  base_model
end

#find_or_create_with(find_attributes, create_attributes = {}, adjust_attributes = false) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/hammock/monkey_patches/active_record.rb', line 97

def find_or_create_with(find_attributes, create_attributes = {}, adjust_attributes = false)
  if record = find_or_new_with(find_attributes, create_attributes)
    log "Create failed. #{record.errors.inspect}", :skip => 1 if record.new_record? && !record.save
    log "Adjust failed. #{record.errors.inspect}", :skip => 1 if adjust_attributes && !record.update_attributes(create_attributes)
    record
  end
end

#find_or_new_with(find_attributes, create_attributes = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hammock/monkey_patches/active_record.rb', line 75

def find_or_new_with(find_attributes, create_attributes = {})
  finder = respond_to?(:find_with_deleted) ? :find_with_deleted : :find

  if record = send(finder, :first, :conditions => find_attributes.discard(:deleted_at))
    # Found the record, so we can return it, if:
    # (a) the record can't have a stored deletion state,
    # (b) it can, but it's not actually deleted,
    # (c) it is deleted, but we want to find one that's deleted, or
    # (d) we don't want a deleted record, and undestruction succeeds.
    if (finder != :find_with_deleted) || !record.deleted? || create_attributes[:deleted_at] || record.undestroy
      record
    end
  else
    creating_class = if create_attributes[:type].is_a?(ActiveRecord::Base)
      create_attributes.delete(:type)
    else
      self
    end
    creating_class.new_with create_attributes.merge(find_attributes)
  end
end

#nestable_reflectionsObject



24
25
26
27
28
29
30
# File 'lib/hammock/monkey_patches/active_record.rb', line 24

def nestable_reflections
  if nestable_routing_resources.nil?
    reflections
  else
    reflections.dragnet *nestable_routing_resources
  end
end

#new_with(attributes) ⇒ Object



17
18
19
20
21
22
# File 'lib/hammock/monkey_patches/active_record.rb', line 17

def new_with attributes
  default_attributes.merge(attributes).inject(new) {|record,(k,v)|
    record.send "#{k}=", v
    record
  }
end

#param_keyObject



50
51
52
# File 'lib/hammock/monkey_patches/active_record.rb', line 50

def param_key
  "#{base_model}_id"
end

#record?Boolean

Returns:

  • (Boolean)


62
# File 'lib/hammock/monkey_patches/active_record.rb', line 62

def record?; false end

#reset_cached_column_infoObject



70
71
72
73
# File 'lib/hammock/monkey_patches/active_record.rb', line 70

def reset_cached_column_info
  reset_column_information
  subclasses.each &:reset_cached_column_info
end

#resourceObject



37
38
39
# File 'lib/hammock/monkey_patches/active_record.rb', line 37

def resource
  base_class
end

#resource?Boolean

Returns:

  • (Boolean)


63
# File 'lib/hammock/monkey_patches/active_record.rb', line 63

def resource?; true end

#resource_nameObject



45
46
47
48
# File 'lib/hammock/monkey_patches/active_record.rb', line 45

def resource_name
  # TODO almost certainly a better way to do this
  base_class.to_s.pluralize.underscore
end

#resource_symObject



41
42
43
# File 'lib/hammock/monkey_patches/active_record.rb', line 41

def resource_sym
  resource_name.to_sym
end

#sorterObject



32
33
34
35
# File 'lib/hammock/monkey_patches/active_record.rb', line 32

def sorter
  # TODO updated_at DESC
  proc {|record| record.id }
end

#update_statement(set_clause, where_clause) ⇒ Object



65
66
67
68
# File 'lib/hammock/monkey_patches/active_record.rb', line 65

def update_statement set_clause, where_clause
  statement = "UPDATE #{table_name} SET #{set_clause} WHERE #{send :sanitize_sql_array, where_clause}"
  connection.update statement
end