Class: RefinerySetting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
vendor/plugins/refinery_settings/app/models/refinery_setting.rb

Constant Summary collapse

REPLACEMENTS =

Below is not very nice, but seems to be required The problem is when Rails serialises a fields like booleans it doesn’t retreieve it back out as a boolean it just returns a string. This code maps the two boolean values correctly so a boolean is returned

{"true" => true, "false" => false}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object



39
40
41
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 39

def self.[](name)
  self.find_by_name(name.to_s).value rescue nil
end

.[]=(name, value) ⇒ Object



43
44
45
46
47
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 43

def self.[]=(name, value)
  setting = find_or_create_by_name(name.to_s)
  setting.value = value
  setting.save!
end

.find_or_set(name, the_value) ⇒ Object



34
35
36
37
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 34

def self.find_or_set(name, the_value)
  setting = find_or_create_by_name(:name => name.to_s, :value => the_value)
  setting.value
end

.method_missing(method, *args) ⇒ Object

Access method that allows dot notation to work. say you had a setting called “site_name”. You could access that by going RefinerySetting but with this you can also access that by going RefinerySettting.site_name



22
23
24
25
26
27
28
29
30
31
32
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 22

def self.method_missing(method, *args)
  method_name = method.to_s
  super(method, *args)

rescue NoMethodError
  if method_name =~ /=$/
    self[method_name.gsub('=', '')] = args.first
  else
    self[method_name]
  end
end

.per_pageObject

Number of settings to show per page when using will_paginate



9
10
11
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 9

def self.per_page
  10
end

Instance Method Details

#titleObject

prettier version of the name. site_name becomes Site Name



15
16
17
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 15

def title
  self.name.titleize
end

#valueObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 58

def value
  current_value = self[:value]

  unless current_value.nil?
    # This bit handles true and false so that true and false are actually returned
    # not "0" and "1"
    REPLACEMENTS.each do |current, new_value|
      current_value = new_value if current_value == current
    end

    # converts the serialised value back to an integer
    # if the value is an integer
    begin
      if current_value.to_i.to_s == current_value
        current_value = current_value.to_i  
      end
    rescue
      current_value
    end
  end

  return current_value
end

#value=(new_value) ⇒ Object



82
83
84
85
86
# File 'vendor/plugins/refinery_settings/app/models/refinery_setting.rb', line 82

def value=(new_value)
  # must convert to string if true or false supplied otherwise it becomes 0 or 1, unfortunately.
  new_value = new_value.to_s if ["trueclass","falseclass"].include?(new_value.class.to_s.downcase)
  self[:value] = new_value
end