Class: Locomotive::Mounter::Models::ContentField
- Defined in:
- lib/locomotive/mounter/models/content_field.rb
Instance Attribute Summary collapse
-
#_destroy ⇒ Object
other accessors ##.
Attributes inherited from Base
#_id, #created_at, #mounting_point, #updated_at
Instance Method Summary collapse
-
#find_select_option(name_or_id) ⇒ Object
Find a select option by its name IN the current locale.
-
#initialize ⇒ Object
constructor
callbacks ##.
-
#is_relationship? ⇒ Boolean
Tell if it describes a relationship (belongs_to, many_to_many, has_many) or not.
-
#klass ⇒ Object
Return the content type matching the class_name / target attribute.
-
#klass=(content_type) ⇒ Object
Set directly the content type matching the class_name.
-
#label ⇒ Object
fields ##.
-
#matches?(id_or_name) ⇒ Boolean
Tell if the id, the name of the field or other property based on its name (like formatted_<date field>) matches the parameter.
-
#prepare_attributes ⇒ Object
methods ##.
-
#to_hash ⇒ Hash
Instead of returning a simple hash, it returns a hash with name as the key and the remaining attributes as the value.
-
#to_params ⇒ Hash
Return the params used for the API.
Methods inherited from Base
Methods included from Fields
#[], #attributes, #attributes_with_translations, #localized_field?, #to_yaml, #translated_in, #translated_in?, #write_attributes
Constructor Details
#initialize ⇒ Object
callbacks ##
35 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 35 set_callback :initialize, :after, :prepare_attributes |
Instance Attribute Details
#_destroy ⇒ Object
other accessors ##
38 39 40 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 38 def _destroy @_destroy end |
Instance Method Details
#find_select_option(name_or_id) ⇒ Object
Find a select option by its name IN the current locale.
103 104 105 106 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 103 def find_select_option(name_or_id) return nil if self..blank? self..detect { |option| option.name.to_s == name_or_id.to_s || option._id == name_or_id } end |
#is_relationship? ⇒ Boolean
Tell if it describes a relationship (belongs_to, many_to_many, has_many) or not.
52 53 54 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 52 def is_relationship? %w(belongs_to has_many many_to_many).include?(self.type.to_s) end |
#klass ⇒ Object
Return the content type matching the class_name / target attribute
79 80 81 82 83 84 85 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 79 def klass (@klass ||= self.mounting_point.content_types[self.class_name]).tap do |klass| if klass.nil? raise UnknownContentTypeException.new("unknow content type #{self.class_name}") end end end |
#klass=(content_type) ⇒ Object
Set directly the content type matching the class_name.
91 92 93 94 95 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 91 def klass=(content_type) if self.class_name == content_type.slug @klass = content_type end end |
#label ⇒ Object
fields ##
8 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 8 field :label |
#matches?(id_or_name) ⇒ Boolean
Tell if the id, the name of the field or other property based on its name (like formatted_<date field>) matches the parameter.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 63 def matches?(id_or_name) default = [self._id, self.name] list = default + (case self.type.to_sym when :date, :date_time then ["formatted_#{self.name}"] else [] end) list.include?(id_or_name.to_s) end |
#prepare_attributes ⇒ Object
methods ##
42 43 44 45 46 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 42 def prepare_attributes self.label ||= self.name.try(:humanize) self.type = self.type.to_sym self.sanitize end |
#to_hash ⇒ Hash
Instead of returning a simple hash, it returns a hash with name as the key and the remaining attributes as the value.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 140 def to_hash hash = super.delete_if { |k, v| %w(name position).include?(k) } # class_name is chosen over class_slug if self.is_relationship? hash['class_name'] ||= hash['class_slug'] hash.delete('class_slug') end # select options if self.type == :select hash['select_options'] = self. end { self.name => hash } end |
#to_params ⇒ Hash
Return the params used for the API.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/locomotive/mounter/models/content_field.rb', line 112 def to_params params = self.filter_attributes %w(label name type hint position required localized unique) # we set the _id / _destroy attributes for embedded documents params[:_id] = self._id if self.persisted? params[:_destroy] = self._destroy if self._destroy case self.type when :text params[:text_formatting] = self.text_formatting when :select params[:raw_select_options] = self..map(&:to_params) when :belongs_to params[:class_name] = self.class_name when :has_many, :many_to_many %w(class_name inverse_of order_by ui_enabled).each do |name| params[name.to_sym] = self.send(name.to_sym) end end params end |