Class: Locomotive::CurrentSiteMetafieldsHelper::SchemaField

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/locomotive/current_site_metafields_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(site, namespace, attributes) ⇒ SchemaField

Returns a new instance of SchemaField.


58
59
60
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 58

def initialize(site, namespace, attributes)
  @site, @namespace, @attributes = site, namespace, attributes
end

Instance Method Details

#default_input_optionsObject


106
107
108
109
110
111
112
113
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 106

def default_input_options
  {
    label:      self.label,
    hint:       self.hint.try(:html_safe),
    as:         self.type,
    required:   false
  }
end

#hintObject


70
71
72
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 70

def hint
  t(@attributes['hint'])
end

#input_optionsObject


83
84
85
86
87
88
89
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 83

def input_options
  case type
  when :select then { collection: select_collection }
  else
    {}
  end.merge(default_input_options)
end

#labelObject


66
67
68
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 66

def label
  t(@attributes['label'] || @attributes['name'].humanize)
end

#nameObject


62
63
64
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 62

def name
  t(@attributes['name']).downcase.underscore.gsub(' ', '_')
end

#positionObject


98
99
100
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 98

def position
  @attributes['position']
end

#select_collectionObject


91
92
93
94
95
96
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 91

def select_collection
  @attributes['select_options'].map do |name, label|
    label = { 'default' => name.humanize } if label.blank?
    [(t(label) || name).html_safe, name]
  end
end

#typeObject


74
75
76
77
78
79
80
81
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 74

def type
  @type ||= case (type = @attributes['type'].try(:to_sym))
  when :boolean then :toggle
  when :text    then :rte
  else
    type || :string
  end
end

#valueObject


102
103
104
# File 'app/helpers/locomotive/current_site_metafields_helper.rb', line 102

def value
  t((@site.metafields[@namespace] || {})[name], ::Mongoid::Fields::I18n.locale.to_s)
end