class ActiveObjects

Public Class Methods

i18n_scope() click to toggle source
# File lib/active_objects.rb, line 5
def i18n_scope
  :activerecord
end
lookup_ancestors() click to toggle source
# File lib/active_objects.rb, line 9
def lookup_ancestors
  [new.send(:object_class)]
end
new(attr = {}) click to toggle source
Calls superclass method
# File lib/active_objects.rb, line 14
def initialize(attr = {})
  generate_attr_accessors
  if attr[:id].present?
    @object = object_class.find(attr[:id])
    generate_instances_for @object
  else
    super(attr)
  end
end

Public Instance Methods

save() click to toggle source
# File lib/active_objects.rb, line 24
def save
  if valid?
    persist!
  else
    false
  end
end
update(attr) click to toggle source
# File lib/active_objects.rb, line 32
def update(attr)
  assign_attributes(attr)
  if valid?
    update_form!
  else
    false
  end
end

Private Instance Methods

generate_attr_accessors() click to toggle source
# File lib/active_objects.rb, line 55
def generate_attr_accessors
  permitted_attributes.each do |attr|
    class_eval { attr_accessor attr }
  end
end
generate_instances_for(_object) click to toggle source
# File lib/active_objects.rb, line 61
def generate_instances_for(_object)
  permitted_attributes.each do |attr|
    instance_variable_set("@#{attr}", @object.send(attr))
  end
end
hash_attributes() click to toggle source
# File lib/active_objects.rb, line 51
def hash_attributes
  {}.tap { |hash| permitted_attributes.each { |attr| hash[attr] = send(attr) } }
end
object_class() click to toggle source
# File lib/active_objects.rb, line 43
def object_class
  raise 'Not implemented object_class method'
end
permitted_attributes() click to toggle source
# File lib/active_objects.rb, line 47
def permitted_attributes
  raise 'Not implemented permitted_attributes method'
end
persist!() click to toggle source
# File lib/active_objects.rb, line 67
def persist!
  object_class.create hash_attributes
end
update_form!() click to toggle source
# File lib/active_objects.rb, line 71
def update_form!
  @object.update hash_attributes
end