Module: Hokusai::Templatable::ClassMethods
- Defined in:
- lib/hokusai.rb
Instance Method Summary collapse
-
#from_template(template, &block) ⇒ Object
Build a new, unsaved instance of the model (and any included associations) from the template supplied.
-
#template(*template_columns, **options) ⇒ Object
Define the template specification for the model.
Instance Method Details
#from_template(template, &block) ⇒ Object
Build a new, unsaved instance of the model (and any included associations) from the template supplied. The block will be called with the new instance.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hokusai.rb', line 48 def from_template(template, &block) if template.is_a?(Array) template.map { |tpl| from_template(tpl, &block) } else new_attrs = template.slice(*🌊[:columns]) template.slice(*🌊[:associations]).each do |association, association_template| new_attrs[association] = reflect_on_association(association).klass.from_template(association_template) end new(new_attrs, &block) end end |
#template(*template_columns, **options) ⇒ Object
Define the template specification for the model.
35 36 37 38 39 40 41 42 43 |
# File 'lib/hokusai.rb', line 35 def template(*template_columns, **) template_columns = Array(template_columns).map(&:to_s) included_associations = Array([:include]).map(&:to_s) self.🌊 = { columns: template_columns, associations: included_associations, } end |