Class: SimpleFormClass::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::Validations
Defined in:
lib/simple_form_class/base.rb

Constant Summary collapse

MANDATORY_OWNER_METHODS =
[ :attributes, :attributes=, :valid?, :save ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil, options = {}) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_form_class/base.rb', line 20

def initialize(params = nil, options = {})
  run_callbacks :initialize do
    @params = params || {}
    @options = options

    yield self if block_given?

    check_if_sane_owners!
    self.attributes = @params
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/simple_form_class/base.rb', line 17

def options
  @options
end

#paramsObject Also known as: attributes

Returns the value of attribute params.



14
15
16
# File 'lib/simple_form_class/base.rb', line 14

def params
  @params
end

Class Method Details

.add_owner(owner) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/simple_form_class/base.rb', line 100

def self.add_owner owner
  @owners ||= []

  unless @owners.include? owner
    @owners << owner

    attr_accessor owner
  end
end

.field(field_name, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/simple_form_class/base.rb', line 40

def self.field field_name, options = {}
  add_owner options[:owner]

  @fields ||= {}
  @fields[field_name] = options

  if options[:owner] == :self
    attr_accessor field_name
  else
    delegate field_name, "#{field_name}=", :to => options[:owner], :null => false
  end
end

.fieldsObject



60
61
62
63
64
# File 'lib/simple_form_class/base.rb', line 60

def self.fields
  @fields ||= {}
  @fields.merge! superclass.fields if superclass.respond_to? :fields
  @fields
end

.fields_for_owner(owner) ⇒ Object



74
75
76
# File 'lib/simple_form_class/base.rb', line 74

def self.fields_for_owner owner
  fields.reject{|k,v| not v[:owner] == owner}.keys
end

.ownersObject



54
55
56
57
58
# File 'lib/simple_form_class/base.rb', line 54

def self.owners
  @owners ||= []
  @owners = @owners + superclass.owners if superclass.respond_to? :owners
  @owners.uniq
end

.permitted_fields_for_owner(owner) ⇒ Object



78
79
80
# File 'lib/simple_form_class/base.rb', line 78

def self.permitted_fields_for_owner owner
  fields.reject{|k,v| not (v[:owner] == owner and v[:write])}.keys
end

Instance Method Details

#owners(*args) ⇒ Object



70
71
72
# File 'lib/simple_form_class/base.rb', line 70

def owners(*args)
  owner_hash(*args).values.compact
end

#persisted?Boolean

yes, this method is needed, form breaks without it check why later

Returns:

  • (Boolean)


34
35
36
# File 'lib/simple_form_class/base.rb', line 34

def persisted?
  false
end

#save(*args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/simple_form_class/base.rb', line 82

def save(*args)
  validate = options.has_key?(:validate) ? options[:validate] : true

  if validate
    return false unless valid?
  end

  ActiveRecord::Base.transaction do
    run_callbacks :save do
        not owners(except_self: true).map{ |owner| owner.save(*args) }.include?(false)
    end
  end
end

#save!Object



96
97
98
# File 'lib/simple_form_class/base.rb', line 96

def save!
  save || raise(ActiveRecord::RecordInvalid.new(self))
end