Class: DigitalOpera::FormObject::Base
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/digital_opera/form_object/base.rb
Class Attribute Summary collapse
-
.fields ⇒ Object
Returns the value of attribute fields.
Class Method Summary collapse
- .add_to_fields(field) ⇒ Object
-
.field(*args, options) ⇒ Object
Class Methods —————————-.
Instance Method Summary collapse
- #assign_attributes(args) ⇒ Object
- #fields ⇒ Object
-
#initialize(h = {}) ⇒ Base
constructor
A new instance of Base.
- #persisted? ⇒ Boolean
- #save ⇒ Object
Constructor Details
#initialize(h = {}) ⇒ Base
Returns a new instance of Base.
15 16 17 |
# File 'lib/digital_opera/form_object/base.rb', line 15 def initialize(h={}) assign_attributes(h) end |
Class Attribute Details
.fields ⇒ Object
Returns the value of attribute fields.
12 13 14 |
# File 'lib/digital_opera/form_object/base.rb', line 12 def fields @fields end |
Class Method Details
.add_to_fields(field) ⇒ Object
63 64 65 66 |
# File 'lib/digital_opera/form_object/base.rb', line 63 def self.add_to_fields(field) @fields = @fields || [] @fields << field end |
.field(*args, options) ⇒ Object
Class Methods —————————-
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/digital_opera/form_object/base.rb', line 47 def self.field(*args, ) if .present? && !.is_a?(Hash) args << end args.each do |field| if .is_a?(Hash) && [:date] == true date_field(field) else send :attr_accessor, field end add_to_fields field end end |
Instance Method Details
#assign_attributes(args) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/digital_opera/form_object/base.rb', line 31 def assign_attributes(args) args.each do |k,v| if respond_to?("#{k}=") send("#{k}=", v) end end end |
#fields ⇒ Object
39 40 41 42 43 |
# File 'lib/digital_opera/form_object/base.rb', line 39 def fields self.class.ancestors.map do |a| a.fields if a.respond_to? :fields end.flatten.compact end |
#persisted? ⇒ Boolean
19 20 21 |
# File 'lib/digital_opera/form_object/base.rb', line 19 def persisted? false end |
#save ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/digital_opera/form_object/base.rb', line 23 def save if valid? persist! else false end end |