Class: Redtape::ModelFactory
- Inherits:
-
Object
- Object
- Redtape::ModelFactory
- Defined in:
- lib/redtape/model_factory.rb
Instance Attribute Summary collapse
-
#attr_whitelist ⇒ Object
readonly
Returns the value of attribute attr_whitelist.
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#records_to_save ⇒ Object
readonly
Returns the value of attribute records_to_save.
-
#top_level_name ⇒ Object
readonly
Returns the value of attribute top_level_name.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ ModelFactory
constructor
A new instance of ModelFactory.
- #populate_model ⇒ Object
- #save! ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ ModelFactory
Returns a new instance of ModelFactory.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/redtape/model_factory.rb', line 5 def initialize(args = {}) assert_inputs(args) @attrs = args[:attrs] @attr_whitelist = NullAttrWhitelist.new @controller = args[:controller] @records_to_save = [] @top_level_name = @attr_whitelist.top_level_name || args[:top_level_name] || default_top_level_name_from(controller) if args[:whitelisted_attrs] @attr_whitelist = AttributeWhitelist.new(args[:whitelisted_attrs]) end end |
Instance Attribute Details
#attr_whitelist ⇒ Object (readonly)
Returns the value of attribute attr_whitelist.
3 4 5 |
# File 'lib/redtape/model_factory.rb', line 3 def attr_whitelist @attr_whitelist end |
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
3 4 5 |
# File 'lib/redtape/model_factory.rb', line 3 def attrs @attrs end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
3 4 5 |
# File 'lib/redtape/model_factory.rb', line 3 def controller @controller end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/redtape/model_factory.rb', line 3 def model @model end |
#records_to_save ⇒ Object (readonly)
Returns the value of attribute records_to_save.
3 4 5 |
# File 'lib/redtape/model_factory.rb', line 3 def records_to_save @records_to_save end |
#top_level_name ⇒ Object (readonly)
Returns the value of attribute top_level_name.
3 4 5 |
# File 'lib/redtape/model_factory.rb', line 3 def top_level_name @top_level_name end |
Instance Method Details
#populate_model ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/redtape/model_factory.rb', line 21 def populate_model @model = find_or_create_root_model populators = [ Populator::Root.new(root_populator_args) ] populators.concat( create_populators_for(model, attrs.values.first).flatten ) populators.each do |p| p.call end violations = populators.map(&:whitelist_failures).flatten if violations.present? errors = violations.join(", ") fail WhitelistViolationError, "Form supplied non-whitelisted attrs #{errors}" end @model end |
#save! ⇒ Object
42 43 44 45 |
# File 'lib/redtape/model_factory.rb', line 42 def save! model.save! records_to_save.each(&:save!) end |