Class: Templatr::Template

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/templatr/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.templatable_classObject



8
9
10
# File 'app/models/templatr/template.rb', line 8

def self.templatable_class
  self.to_s[/[A-Z][a-z]+/].constantize
end

Instance Method Details

#attribute_present?(attribute) ⇒ Boolean

In order to make common fields appear on a new form, we need to make the has_many association think that it should load them from the database We do so by pretending we have a primary key, knowing that it will evaluate to null

Returns:

  • (Boolean)


23
24
25
# File 'app/models/templatr/template.rb', line 23

def attribute_present?(attribute)
  attribute.to_s == 'common_fields_fake_foreign_key' ? true : super
end

#common_fields_attributes=(nested_attributes) ⇒ Object

Ensure all nested attributes for common fields get saved as common fields, and not as template fields



28
29
30
31
32
33
# File 'app/models/templatr/template.rb', line 28

def common_fields_attributes=(nested_attributes)
  nested_attributes.values.each do |attributes|
    common_field = common_fields.find {|field| field.id.to_s == attributes[:id] && attributes[:id].present? } || common_fields.build
    assign_to_or_mark_for_destruction(common_field, attributes, true, {})
  end
end

#template_fieldsObject

Combined common and default fields



13
14
15
# File 'app/models/templatr/template.rb', line 13

def template_fields
  common_fields + default_fields
end

#to_sObject



17
18
19
# File 'app/models/templatr/template.rb', line 17

def to_s
  self.name
end