Class: Templatr::Field
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Templatr::Field
- Defined in:
- app/models/templatr/field.rb
Class Method Summary collapse
-
.reserved_fields ⇒ Object
Reserved field names that the user is not allowed to use “Type” is reserved to differentiate between Templates.
- .search_suggestions(value = true) ⇒ Object
- .templatable_class ⇒ Object
- .valid_field_types ⇒ Object
- .valid_migration_path?(from, to) ⇒ Boolean
-
.valid_migration_paths ⇒ Object
The valid ways to migrate data between field types.
Instance Method Summary collapse
-
#attribute_type ⇒ Object
What attribute type should glint use to store this field’s values.
- #boolean? ⇒ Boolean
-
#can_change_type? ⇒ Boolean
Don’t allow changes to the type if the field is saved.
- #common? ⇒ Boolean
- #facet? ⇒ Boolean
- #facet_name ⇒ Object
-
#field_type=(value) ⇒ Object
Coerce the field_type to a string at all times so testing for it is easier.
- #float? ⇒ Boolean
- #integer? ⇒ Boolean
- #integer_with_uncertainty? ⇒ Boolean
- #param ⇒ Object
- #scalar? ⇒ Boolean
- #select? ⇒ Boolean
- #select_multiple? ⇒ Boolean
- #select_one? ⇒ Boolean
- #string? ⇒ Boolean
- #text? ⇒ Boolean
- #to_s ⇒ Object
- #valid_migration_paths ⇒ Object
- #vector? ⇒ Boolean
Class Method Details
.reserved_fields ⇒ Object
Reserved field names that the user is not allowed to use “Type” is reserved to differentiate between Templates
23 24 25 |
# File 'app/models/templatr/field.rb', line 23 def self.reserved_fields [:type] + templatable_class.attribute_names.collect(&:to_sym) + templatable_class.reflect_on_all_associations.collect(&:name) end |
.search_suggestions(value = true) ⇒ Object
15 |
# File 'app/models/templatr/field.rb', line 15 def self.search_suggestions(value = true); where(:search_suggestions => value); end |
.templatable_class ⇒ Object
17 18 19 |
# File 'app/models/templatr/field.rb', line 17 def self.templatable_class self.to_s.gsub(/Field\Z/, '').constantize end |
.valid_field_types ⇒ Object
27 28 29 |
# File 'app/models/templatr/field.rb', line 27 def self.valid_field_types %w(string text float integer boolean integer_with_uncertainty select_one select_multiple) end |
.valid_migration_path?(from, to) ⇒ Boolean
38 39 40 41 |
# File 'app/models/templatr/field.rb', line 38 def self.valid_migration_path?(from, to) paths = valid_migration_paths[from] paths && paths.include?(to.to_sym) end |
.valid_migration_paths ⇒ Object
The valid ways to migrate data between field types
32 33 34 35 36 |
# File 'app/models/templatr/field.rb', line 32 def self.valid_migration_paths { :string => [:select_one, :select_multiple, :text], :select_one => [:string, :select_multiple, :text] }.with_indifferent_access end |
Instance Method Details
#attribute_type ⇒ Object
What attribute type should glint use to store this field’s values
101 102 103 |
# File 'app/models/templatr/field.rb', line 101 def attribute_type (float? || integer? || text? || boolean? ? field_type : 'string').to_sym end |
#boolean? ⇒ Boolean
60 |
# File 'app/models/templatr/field.rb', line 60 def boolean?; field_type == 'boolean' end |
#can_change_type? ⇒ Boolean
Don’t allow changes to the type if the field is saved
81 82 83 |
# File 'app/models/templatr/field.rb', line 81 def can_change_type? new_record? || self.class.valid_migration_paths[self.field_type].present? end |
#common? ⇒ Boolean
72 73 74 |
# File 'app/models/templatr/field.rb', line 72 def common? template_id.nil? end |
#facet? ⇒ Boolean
76 77 78 |
# File 'app/models/templatr/field.rb', line 76 def facet? include_in_search_form? || search_suggestions? end |
#facet_name ⇒ Object
105 106 107 |
# File 'app/models/templatr/field.rb', line 105 def facet_name :"field_#{id}" end |
#field_type=(value) ⇒ Object
Coerce the field_type to a string at all times so testing for it is easier
94 95 96 |
# File 'app/models/templatr/field.rb', line 94 def field_type=(value) super(value.to_s) end |
#float? ⇒ Boolean
61 |
# File 'app/models/templatr/field.rb', line 61 def float?; field_type == 'float' end |
#integer? ⇒ Boolean
62 |
# File 'app/models/templatr/field.rb', line 62 def integer?; field_type == 'integer' end |
#integer_with_uncertainty? ⇒ Boolean
63 |
# File 'app/models/templatr/field.rb', line 63 def integer_with_uncertainty?; field_type == 'integer_with_uncertainty' end |
#param ⇒ Object
109 110 111 |
# File 'app/models/templatr/field.rb', line 109 def param (self.disambiguate? ? "#{template.name} #{self.name}" : self.name).downcase # param is always case insensitive end |
#scalar? ⇒ Boolean
85 86 87 |
# File 'app/models/templatr/field.rb', line 85 def scalar? string? || text? || boolean? || float? || integer? || integer_with_uncertainty? end |
#select? ⇒ Boolean
64 |
# File 'app/models/templatr/field.rb', line 64 def select?; field_type == 'select_one' || field_type == 'select_multiple' end |
#select_multiple? ⇒ Boolean
66 |
# File 'app/models/templatr/field.rb', line 66 def select_multiple?; field_type == 'select_multiple' end |
#select_one? ⇒ Boolean
65 |
# File 'app/models/templatr/field.rb', line 65 def select_one?; field_type == 'select_one' end |
#string? ⇒ Boolean
58 |
# File 'app/models/templatr/field.rb', line 58 def string?; field_type == 'string' end |
#text? ⇒ Boolean
59 |
# File 'app/models/templatr/field.rb', line 59 def text?; field_type == 'text' end |
#to_s ⇒ Object
68 69 70 |
# File 'app/models/templatr/field.rb', line 68 def to_s self.name end |
#valid_migration_paths ⇒ Object
43 44 45 |
# File 'app/models/templatr/field.rb', line 43 def valid_migration_paths new_record? ? self.class.valid_field_types : [self.field_type] + Array(self.class.valid_migration_paths[self.field_type]) end |
#vector? ⇒ Boolean
89 90 91 |
# File 'app/models/templatr/field.rb', line 89 def vector? !scalar? end |