Class: StudyEngine::Form
- Inherits:
-
Object
- Object
- StudyEngine::Form
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Validations
- Defined in:
- app/models/study_engine/form.rb
Direct Known Subclasses
Class Method Summary collapse
- .attributes(*attributes) ⇒ Object
- .lookup_ancestors ⇒ Object
- .model_name ⇒ Object
- .new_from_model(model) ⇒ Object
- .wrapped_class ⇒ Object
- .wraps(model) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(attributes = {}, model = self.class.wrapped_class.new) ⇒ Form
constructor
A new instance of Form.
- #save ⇒ Object
Constructor Details
#initialize(attributes = {}, model = self.class.wrapped_class.new) ⇒ Form
Returns a new instance of Form.
38 39 40 41 42 43 |
# File 'app/models/study_engine/form.rb', line 38 def initialize attributes = {}, model = self.class.wrapped_class.new @model = model attributes.each do |name, value| send("#{name}=", value) end end |
Class Method Details
.attributes(*attributes) ⇒ Object
26 27 28 29 30 31 |
# File 'app/models/study_engine/form.rb', line 26 def self.attributes *attributes attributes.each do |attribute| attributes_list << attribute attr_accessor attribute end end |
.lookup_ancestors ⇒ Object
20 21 22 |
# File 'app/models/study_engine/form.rb', line 20 def self.lookup_ancestors [self] end |
.model_name ⇒ Object
12 13 14 |
# File 'app/models/study_engine/form.rb', line 12 def self.model_name ActiveModel::Name.new(wrapped_class) end |
.new_from_model(model) ⇒ Object
33 34 35 36 |
# File 'app/models/study_engine/form.rb', line 33 def self.new_from_model model attributes = model.attributes.slice(*attributes_list.map(&:to_s)) new attributes end |
.wrapped_class ⇒ Object
16 17 18 |
# File 'app/models/study_engine/form.rb', line 16 def self.wrapped_class @wraps.to_s.classify.constantize end |
.wraps(model) ⇒ Object
8 9 10 |
# File 'app/models/study_engine/form.rb', line 8 def self.wraps(model) @wraps = model end |
Instance Method Details
#attributes ⇒ Object
45 46 47 48 49 50 |
# File 'app/models/study_engine/form.rb', line 45 def attributes attributes_list.reduce({}) do |hash, key| hash[key] = send(key) hash end end |
#save ⇒ Object
52 53 54 |
# File 'app/models/study_engine/form.rb', line 52 def save @model.update_attributes(attributes) if valid? end |