Class: SimpleFormClass::Base
- Inherits:
-
Object
- Object
- SimpleFormClass::Base
- 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
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
(also: #attributes)
Returns the value of attribute params.
Class Method Summary collapse
- .add_owner(owner) ⇒ Object
- .field(field_name, options = {}) ⇒ Object
- .fields ⇒ Object
- .fields_for_owner(owner) ⇒ Object
- .owners ⇒ Object
- .permitted_fields_for_owner(owner) ⇒ Object
Instance Method Summary collapse
-
#initialize(params = nil, options = {}) ⇒ Base
constructor
A new instance of Base.
- #owners(*args) ⇒ Object
-
#persisted? ⇒ Boolean
yes, this method is needed, form breaks without it check why later.
- #save(*args) ⇒ Object
- #save! ⇒ Object
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, = {}) run_callbacks :initialize do @params = params || {} @options = yield self if block_given? check_if_sane_owners! self.attributes = @params end end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
17 18 19 |
# File 'lib/simple_form_class/base.rb', line 17 def @options end |
#params ⇒ Object 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, = {} add_owner [:owner] @fields ||= {} @fields[field_name] = if [:owner] == :self attr_accessor field_name else delegate field_name, "#{field_name}=", :to => [:owner], :null => false end end |
.fields ⇒ Object
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 |
.owners ⇒ Object
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
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 = .has_key?(:validate) ? [: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 |