Class: Locomotive::Mounter::Models::ContentField

Inherits:
Base
  • Object
show all
Defined in:
lib/locomotive/mounter/models/content_field.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#_id, #created_at, #mounting_point, #updated_at

Instance Method Summary collapse

Methods inherited from Base

#persisted?

Methods included from Fields

#[], #attributes, #attributes_with_translations, #localized_field?, #to_yaml, #translated_in, #translated_in?, #write_attributes

Constructor Details

#initializeObject

callbacks ##



35
# File 'lib/locomotive/mounter/models/content_field.rb', line 35

set_callback :initialize, :after, :prepare_attributes

Instance Attribute Details

#_destroyObject

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.

Parameters:

  • name_or_id (String / Symbol)

    Name or Id of the option

Returns:

  • (Object)

    The select option or nil if not found



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.select_options.blank?
  self.select_options.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.

Returns:

  • (Boolean)

    True if describing a relationship



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

#klassObject

Return the content type matching the class_name / target attribute

Returns:

  • (Object)

    The matching Content Type



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.

Parameters:

  • The (Object)

    matching Content Type



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

#labelObject

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.

Parameters:

  • name_or_id (String / Symbol)

    Name or Id of the field

Returns:

  • (Boolean)

    True if the current 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_attributesObject

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_hashHash

Instead of returning a simple hash, it returns a hash with name as the key and the remaining attributes as the value.

Returns:

  • (Hash)

    A hash of hash



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.select_options_to_hash
  end

  { self.name => hash }
end

#to_paramsHash

Return the params used for the API.

Returns:

  • (Hash)

    The params



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.select_options.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