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:



136
137
138
139
140
141
142
# File 'app/models/locomotive/content_type.rb', line 136

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

Instance Method Details

#class_name_to_content_type(class_name) ⇒ Object



89
90
91
# File 'app/models/locomotive/content_type.rb', line 89

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

#entriesObject

behaviours ##



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

custom_fields_for :entries

#entries_class_nameString

Get the class name of the entries.

Returns:

  • (String)

    The class name of all the entries



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

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



121
122
123
124
125
126
# File 'app/models/locomotive/content_type.rb', line 121

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



73
74
75
# File 'app/models/locomotive/content_type.rb', line 73

def group_by_field
  self.find_entries_custom_field(self.group_by_field_id)
end

#groupable?Boolean

Returns:



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

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

#label_field_id=(value) ⇒ Object



93
94
95
96
97
98
# File 'app/models/locomotive/content_type.rb', line 93

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



100
101
102
103
104
# File 'app/models/locomotive/content_type.rb', line 100

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_entriesObject



77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/locomotive/content_type.rb', line 77

def list_or_group_entries
  if self.groupable?
    if 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
  end
end

#nameObject

fields ##



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

field :name

#order_by_definition(reverse_order = false) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'app/models/locomotive/content_type.rb', line 54

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:



50
51
52
# File 'app/models/locomotive/content_type.rb', line 50

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

#orderedObject

named scopes ##



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

scope :ordered, :order_by => :updated_at.desc

#ordered_entries(conditions = {}) ⇒ Object



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

def ordered_entries(conditions = {})
  _order_by_definition = (conditions || {}).delete(:order_by).try(:split) || self.order_by_definition
  self.entries.order_by([_order_by_definition]).where(conditions)
end

#siteObject

validations ##



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

belongs_to  :site,      :class_name => 'Locomotive::Site'

#site_idObject

indexes ##



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

index [[:site_id, Mongo::ASCENDING], [:slug, Mongo::ASCENDING]]