Class: Locomotive::ContentType
- Inherits:
-
Object
- Object
- Locomotive::ContentType
- 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
-
.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.
Instance Method Summary collapse
- #class_name_to_content_type(class_name) ⇒ Object
-
#entries ⇒ Object
behaviours ##.
-
#entries_class_name ⇒ String
Get the class name of the entries.
-
#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.
- #group_by_field ⇒ Object
- #groupable? ⇒ Boolean
- #label_field_id=(value) ⇒ Object
- #label_field_name=(value) ⇒ Object
- #list_or_group_entries(options = {}) ⇒ Object
-
#name ⇒ Object
fields ##.
- #order_by_definition(reverse_order = false) ⇒ Object
-
#order_manually? ⇒ Boolean
methods ##.
-
#ordered ⇒ Object
named scopes ##.
- #ordered_entries(conditions = {}) ⇒ Object
-
#site ⇒ Object
validations ##.
Methods included from Extensions::ContentType::ItemTemplate
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
146 147 148 149 150 151 152 |
# File 'app/models/locomotive/content_type.rb', line 146 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
99 100 101 |
# File 'app/models/locomotive/content_type.rb', line 99 def class_name_to_content_type(class_name) self.class.class_name_to_content_type(class_name, self.site) end |
#entries ⇒ Object
behaviours ##
52 |
# File 'app/models/locomotive/content_type.rb', line 52 custom_fields_for :entries |
#entries_class_name ⇒ String
Get the class name of the entries.
120 121 122 |
# File 'app/models/locomotive/content_type.rb', line 120 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.
131 132 133 134 135 136 |
# File 'app/models/locomotive/content_type.rb', line 131 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_field ⇒ Object
79 80 81 |
# File 'app/models/locomotive/content_type.rb', line 79 def group_by_field self.find_entries_custom_field(self.group_by_field_id) end |
#groupable? ⇒ Boolean
75 76 77 |
# File 'app/models/locomotive/content_type.rb', line 75 def groupable? !!self.group_by_field && %w(select belongs_to).include?(group_by_field.type) end |
#label_field_id=(value) ⇒ Object
103 104 105 106 107 108 |
# File 'app/models/locomotive/content_type.rb', line 103 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
110 111 112 113 114 |
# File 'app/models/locomotive/content_type.rb', line 110 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/models/locomotive/content_type.rb', line 83 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 if [:page].nil? self.ordered_entries else self.ordered_entries.page([:page]).per([:per_page]) end end end |
#name ⇒ Object
fields ##
13 |
# File 'app/models/locomotive/content_type.rb', line 13 field :name |
#order_by_definition(reverse_order = false) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'app/models/locomotive/content_type.rb', line 60 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 ##
56 57 58 |
# File 'app/models/locomotive/content_type.rb', line 56 def order_manually? self.order_by == '_position' end |
#ordered ⇒ Object
named scopes ##
35 |
# File 'app/models/locomotive/content_type.rb', line 35 scope :ordered, order_by(updated_at: :desc) |
#ordered_entries(conditions = {}) ⇒ Object
70 71 72 73 |
# File 'app/models/locomotive/content_type.rb', line 70 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 |
#site ⇒ Object
validations ##
25 |
# File 'app/models/locomotive/content_type.rb', line 25 belongs_to :site, class_name: 'Locomotive::Site' |