Class: Locomotive::ContentType

Inherits:
Object
  • Object
show all
Includes:
CustomFields::Source, Extensions::ContentType::DefaultValues, Extensions::ContentType::ItemTemplate, Extensions::ContentType::Sync, Mongoid::Document
Defined in:
app/models/locomotive/content_type.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::ContentType::ItemTemplate

#item_template

Class Method Details

.class_name_to_content_type(class_name, site) ⇒ Locomotive::ContentType

Retrieve from a class name the associated content type within the scope of a site. If no content type is found, the method returns nil

Parameters:

Returns:



182
183
184
185
186
187
188
# File 'app/models/locomotive/content_type.rb', line 182

def self.class_name_to_content_type(class_name, site)
  if class_name =~ /^Locomotive::ContentEntry(.*)/
    site.content_types.find($1)
  else
    nil
  end
end

Instance Method Details

#class_name_to_content_type(class_name) ⇒ Object



135
136
137
# File 'app/models/locomotive/content_type.rb', line 135

def class_name_to_content_type(class_name)
  self.class.class_name_to_content_type(class_name, self.site)
end

#entriesObject

behaviours ##



64
# File 'app/models/locomotive/content_type.rb', line 64

custom_fields_for :entries

#entries_class_nameString

Get the class name of the entries.

Returns:

  • (String)

    The class name of all the entries



156
157
158
# File 'app/models/locomotive/content_type.rb', line 156

def entries_class_name
  self.klass_with_custom_fields(:entries).to_s
end

#find_entries_custom_field(id_or_name) ⇒ Object

Find a custom field describing an entry based on its id in first or its name if not found.

Parameters:

  • id_or_name (String)

    The id of name of the field

Returns:

  • (Object)

    The custom field or nit if not found



167
168
169
170
171
172
# File 'app/models/locomotive/content_type.rb', line 167

def find_entries_custom_field(id_or_name)
  return nil if id_or_name.nil? # bypass the memoization

  _field = self.entries_custom_fields.find(id_or_name) rescue nil
  _field || self.entries_custom_fields.where(name: id_or_name).first
end

#group_by_fieldObject



109
110
111
# File 'app/models/locomotive/content_type.rb', line 109

def group_by_field
  self.find_entries_custom_field(self.group_by_field_id)
end

#groupable?Boolean

Returns:



105
106
107
# File 'app/models/locomotive/content_type.rb', line 105

def groupable?
  !!self.group_by_field && %w(select belongs_to).include?(group_by_field.type)
end

#label_field_id=(value) ⇒ Object



139
140
141
142
143
144
# File 'app/models/locomotive/content_type.rb', line 139

def label_field_id=(value)
  # update the label_field_name if the label_field_id is changed
  new_label_field_name = self.entries_custom_fields.where(_id: value).first.try(:name)
  self.label_field_name = new_label_field_name
  super(value)
end

#label_field_name=(value) ⇒ Object



146
147
148
149
150
# File 'app/models/locomotive/content_type.rb', line 146

def label_field_name=(value)
  # mandatory if we allow the API to set the label field name without an id of the field
  @new_label_field_name = value unless value.blank?
  super(value)
end

#list_or_group_entries(options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/locomotive/content_type.rb', line 123

def list_or_group_entries(options = {})
  if self.groupable?
    if self.group_by_field.type == 'select'
      self.entries.group_by_select_option(self.group_by_field.name, self.order_by_definition)
    else
      group_by_belongs_to_field(self.group_by_field)
    end
  else
    self.ordered_entries(options)
  end
end

#nameObject

fields ##



13
# File 'app/models/locomotive/content_type.rb', line 13

field :name

#order_by_definition(reverse_order = false) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'app/models/locomotive/content_type.rb', line 72

def order_by_definition(reverse_order = false)
  direction = self.order_manually? ? 'asc' : self.order_direction || 'asc'

  if reverse_order
    direction = (direction == 'asc' ? 'desc' : 'asc')
  end

  [order_by_attribute, direction]
end

#order_manually?Boolean

methods ##

Returns:



68
69
70
# File 'app/models/locomotive/content_type.rb', line 68

def order_manually?
  self.order_by == '_position'
end

#orderedObject

named scopes ##



43
# File 'app/models/locomotive/content_type.rb', line 43

scope :ordered, order_by(updated_at: :desc)

#ordered_entries(options = nil) ⇒ Criteria

Order the list of entries, paginate it if requested and filter it.

Parameters:

  • options (Hash) (defaults to: nil)

    Options to filter (where key), order (order_by key) and paginate (page, per_page keys)

Returns:

  • (Criteria)

    A Mongoid criteria if not paginated (array otherwise).



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/locomotive/content_type.rb', line 89

def ordered_entries(options = nil)
  options ||= {}

  # pagination
  page, per_page = options.delete(:page), options.delete(:per_page)

  # order list
  _order_by_definition = (options || {}).delete(:order_by).try(:split) || self.order_by_definition

  # get list
  _entries = self.entries.order_by([_order_by_definition]).where(options[:where] || {})

  # pagination or full list
  !self.order_manually? && page ? _entries.page(page).per(per_page) : _entries
end

#siteObject

validations ##



26
# File 'app/models/locomotive/content_type.rb', line 26

belongs_to  :site,      class_name: 'Locomotive::Site', validate: false

#sortable_columnObject



113
114
115
116
117
118
119
120
121
# File 'app/models/locomotive/content_type.rb', line 113

def sortable_column
  # only the belongs_to field has a special column for relative positionning
  # that's why we don't call groupable?
  if self.group_by_field.try(:type) == 'belongs_to' && self.order_manually?
    "position_in_#{self.group_by_field.name}"
  else
    '_position'
  end
end