Class: Locomotive::ContentFieldPresenter

Inherits:
BasePresenter show all
Defined in:
app/presenters/locomotive/content_field_presenter.rb

Instance Attribute Summary

Attributes inherited from BasePresenter

#__ability, #__depth

Instance Method Summary collapse

Methods inherited from BasePresenter

#_id, #ability?, #after_initialize, #errors, #formatted_time, getters_or_setters_to_hash, getters_to_hash, #html_view?, #include_errors?, setters_to_hash, #true

Methods included from Presentable

#after_initialize, #as_json, #attributes=, #getters, #initialize, #property_options, #setters

Instance Method Details

#class_name=(value) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'app/presenters/locomotive/content_field_presenter.rb', line 55

def class_name=(value)
  if value =~ /^Locomotive::ContentEntry/
    self.__source.class_name = value
  else
    if content_type = self.site.content_types.where(slug: value).first
      self.__source.class_name = content_type.entries_class_name
    end
  end
end

#class_slugObject



51
52
53
# File 'app/presenters/locomotive/content_field_presenter.rb', line 51

def class_slug
  self.content_type.class_name_to_content_type(self.__source.class_name).try(:slug) rescue ::Mongoid::Errors::DocumentNotFound
end

#content_typeObject

other methods ##



67
68
69
# File 'app/presenters/locomotive/content_field_presenter.rb', line 67

def content_type
  self.__source.content_type
end

#nameObject

properties ##



6
# File 'app/presenters/locomotive/content_field_presenter.rb', line 6

properties  :name, :label, :type

#raw_select_optionsObject

other getters / setters ##



30
31
32
33
34
# File 'app/presenters/locomotive/content_field_presenter.rb', line 30

def raw_select_options
  self.__source.select_options.collect do |option|
    { id: option._id, name: option.name_translations, position: position }
  end
end

#raw_select_options=(values) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/presenters/locomotive/content_field_presenter.rb', line 36

def raw_select_options=(values)
  values.each do |attributes|
    # translations ?
    if (name = attributes['name']).is_a?(Hash)
      translations = attributes['name_translations'] = attributes.delete('name')
      name = translations[self.site.default_locale]
    end

    option = self.__source.select_options.where(name: name).first
    option ||= self.__source.select_options.build

    option.attributes = attributes
  end
end

#siteObject



71
72
73
# File 'app/presenters/locomotive/content_field_presenter.rb', line 71

def site
  self.content_type.site
end