Module: Hammock::ActiveRecordPatches::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#base_modelObject



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

def base_model
  base_class.to_s.underscore
end

#descriptionObject



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

def description
  base_model
end

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



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

def find_or_create_with(find_attributes, create_attributes = {}, should_adjust = 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 should_adjust && !record.adjust_attributes(create_attributes)
    record
  end
end

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



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

def find_or_new_with(find_attributes, create_attributes = {}, should_assign = false)
  finder = respond_to?(:find_with_destroyed) ? :find_with_destroyed : :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_destroyed) || !record.deleted? || create_attributes[:deleted_at] || record.restore
      record.attributes = create_attributes if should_assign
      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



28
29
30
31
32
33
34
# File 'lib/hammock/monkey_patches/active_record.rb', line 28

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

#new_with(attributes) ⇒ Object



21
22
23
24
25
26
# File 'lib/hammock/monkey_patches/active_record.rb', line 21

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

#param_keyObject



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

def param_key
  "#{base_model}_id"
end

#record?Boolean

Returns:

  • (Boolean)


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

def record?; false end

#reset_cached_column_infoObject



68
69
70
71
# File 'lib/hammock/monkey_patches/active_record.rb', line 68

def reset_cached_column_info
  reset_column_information
  subclasses.each &:reset_cached_column_info
end

#resourceObject



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

def resource
  base_class
end

#resource?Boolean

Returns:

  • (Boolean)


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

def resource?; true end

#resource_nameObject



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

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

#resource_symObject



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

def resource_sym
  resource_name.to_sym
end

#sorterObject



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

def sorter
  L{|record| -record.updated_at }
end