Class: Refinery::Setting

Inherits:
Core::BaseModel
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/refinery/setting.rb

Constant Summary collapse

FORM_VALUE_TYPES =
[
  ['Multi-line', 'text_area'],
  ['Checkbox', 'check_box']
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_set(name, the_value, options = {}) ⇒ Object Also known as: get_or_set

find_or_set offers a convenient way to



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/refinery/setting.rb', line 38

def find_or_set(name, the_value, options={})
  # Merge default options with supplied options.
  options = {
    scoping: nil,
    restricted: false,
    form_value_type: 'text_area'
  }.merge(options)

  # try to find the setting first
  value = get(name, scoping: options[:scoping])

  # if the setting's value is nil, store a new one using the existing functionality.
  value = set(name, options.merge(value: the_value)) if value.nil?

  # Return what we found.
  value
end

.form_value_typesObject



33
34
35
# File 'app/models/refinery/setting.rb', line 33

def form_value_types
  FORM_VALUE_TYPES
end

.get(name, options = {}) ⇒ Object Also known as: []

Retrieve the current value for the setting whose name is supplied.



59
60
61
62
63
# File 'app/models/refinery/setting.rb', line 59

def get(name, options = {})
  options = {scoping: nil}.update(options)
  where(name: name, scoping: options[:scoping])
    .select(:value).map(&:value).first
end

.per_pageObject

Number of settings to show per page when using will_paginate



29
30
31
# File 'app/models/refinery/setting.rb', line 29

def per_page
  12
end

.set(name, value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/refinery/setting.rb', line 67

def set(name, value)
  return (value.is_a?(Hash) ? value[:value] : value) unless (table_exists? rescue false)

  scoping = (value[:scoping] if value.is_a?(Hash) and value.has_key?(:scoping))
  setting = where(name: name.to_s, scoping: scoping).first_or_initialize

  # you could also pass in {:value => 'something', :scoping => 'somewhere'}
  unless value.is_a?(Hash) and value.has_key?(:value)
    setting.value = value
  else
    # set the value last, so that the other attributes can transform it if required.
    setting.form_value_type = value[:form_value_type] || 'text_area' if setting.respond_to?(:form_value_type)
    setting.scoping = value[:scoping] if value.has_key?(:scoping)
    setting.destroyable = value[:destroyable] if value.has_key?(:destroyable)
    setting.value = value[:value]
  end

  # Save because we're in a setter method.
  setting.save

  # Return the value
  setting.value
end

Instance Method Details

#form_valueObject

form_value is so that on the web interface we can display a sane value.



97
98
99
100
101
102
103
# File 'app/models/refinery/setting.rb', line 97

def form_value
  unless self[:value].blank? or self[:value].is_a?(String)
    YAML::dump(self[:value])
  else
    self[:value]
  end
end

#should_generate_new_friendly_id?Boolean

If name changes tell friendly_id to regenerate slug when saving record

Returns:

  • (Boolean)


10
11
12
# File 'app/models/refinery/setting.rb', line 10

def should_generate_new_friendly_id?
  name_changed?
end

#titleObject



92
93
94
# File 'app/models/refinery/setting.rb', line 92

def title
  self[:title].presence || auto_title
end

#valueObject



105
106
107
# File 'app/models/refinery/setting.rb', line 105

def value
  replacements!(self[:value])
end

#value=(new_value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/refinery/setting.rb', line 109

def value=(new_value)
  # must convert "1" to true and "0" to false when supplied using 'check_box', unfortunately.
  if %w[1 0].include?(new_value) && form_value_type == 'check_box'
    new_value = new_value == "1"
  end

  # must convert to string if true or false supplied otherwise it becomes 0 or 1, unfortunately.
  new_value = new_value.to_s if [true, false].include?(new_value)

  super
end