Class: Preference

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/preference.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2016 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: preferences

boolean_value     :boolean
created_at        :datetime         not null
creator_id        :integer
decimal_value     :decimal(19, 4)
id                :integer          not null, primary key
integer_value     :integer
lock_version      :integer          default(0), not null
name              :string           not null
nature            :string           not null
record_value_id   :integer
record_value_type :string
string_value      :text
updated_at        :datetime         not null
updater_id        :integer
user_id           :integer

Constant Summary collapse

@@conversions =
{ float: :decimal, true_class: :boolean, false_class: :boolean, fixnum: :integer }

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ekylibre::Record::Base

#already_updated?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Class Method Details

.[](name) ⇒ Object


108
109
110
# File 'app/models/preference.rb', line 108

def [](name)
  get!(name).value
end

.check!Object


85
86
87
88
89
# File 'app/models/preference.rb', line 85

def check!
  reference.keys.each do |pref|
    get(pref)
  end
end

.get(name, default_value = nil, nature = nil) ⇒ Object

Try to find preference and create it with default value only if it doesn't exist


131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/preference.rb', line 131

def get(name, default_value = nil, nature = nil)
  name = name.to_s
  preference = Preference.find_by(name: name)
  unless preference
    attributes = { name: name, nature: nature, value: default_value }
    if reference.key?(name)
      attributes[:nature] = reference[name][:nature]
      attributes[:value] ||= reference[name][:default]
    end
    preference = create!(attributes)
  end
  preference
end

.get!(name) ⇒ Object

Raise an exception if preference cannot be found or initialized with the reference


114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/preference.rb', line 114

def get!(name)
  name = name.to_s
  preference = Preference.find_by(name: name)
  unless preference
    if reference.key?(name)
      preference = new(name: name, nature: reference[name][:nature])
      preference.value = reference[name][:default] if reference[name][:default]
      preference.save!
    else
      raise ArgumentError, "Undefined preference: #{name}"
    end
  end
  preference
end

.prefer(name, nature, default_value = nil) ⇒ Object


77
78
79
80
81
82
83
# File 'app/models/preference.rb', line 77

def prefer(name, nature, default_value = nil)
  @@reference ||= HashWithIndifferentAccess.new
  unless self.nature.values.include?(nature.to_s)
    raise ArgumentError, "Nature (#{nature.inspect}) is unacceptable. #{self.nature.values.to_sentence} are accepted."
  end
  @@reference[name] = { name: :name, nature: nature.to_sym, default: default_value }
end

.set!(name, value, nature = nil) ⇒ Object

Find and set preference with given value


151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/preference.rb', line 151

def set!(name, value, nature = nil)
  name = name.to_s
  preference = Preference.find_by(name: name)
  if preference
    preference.reload
  else
    attributes = { name: name, nature: nature }
    attributes[:nature] = reference[name][:nature] if reference.key?(name)
    preference = new(attributes)
  end
  preference.value = value
  preference.save!
  preference
end

.type_to_nature(object) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/preference.rb', line 91

def type_to_nature(object)
  klass = object.class.to_s
  if object.is_a?(Nomen::Item) && nature = object.nomenclature.name.to_s.singularize.to_sym && nature.values.include?(nature)
    nature
  elsif %w(String Symbol NilClass).include? klass
    :string
  elsif %w(Integer Fixnum Bignum).include? klass
    :integer
  elsif %w(TrueClass FalseClass Boolean).include? klass
    :boolean
  elsif ['BigDecimal'].include? klass
    :decimal
  else
    :record
  end
end

.value(name, default_value = nil, nature = nil) ⇒ Object

Returns value of the given preference


146
147
148
# File 'app/models/preference.rb', line 146

def value(name, default_value = nil, nature = nil)
  get(name, default_value, nature).value
end

Instance Method Details

#human_name(locale = nil) ⇒ Object Also known as: label


207
208
209
# File 'app/models/preference.rb', line 207

def human_name(locale = nil)
  "preferences.#{name}".t(locale: locale)
end

#modelObject


212
213
214
# File 'app/models/preference.rb', line 212

def model
  record? ? record_value_type.constantize : nil
end

#set(object) ⇒ Object


196
197
198
199
# File 'app/models/preference.rb', line 196

def set(object)
  self.value = object
  save
end

#set!(object) ⇒ Object


201
202
203
204
205
# File 'app/models/preference.rb', line 201

def set!(object)
  reload if already_updated?
  self.value = object
  save!
end

#valueObject

Returns basically the value of the preference


187
188
189
# File 'app/models/preference.rb', line 187

def value
  send(value_attribute)
end

#value=(object) ⇒ Object


191
192
193
194
# File 'app/models/preference.rb', line 191

def value=(object)
  self.nature ||= self.class.type_to_nature(object)
  send(value_attribute + '=', object)
end

#value_attributeObject

Returns the name of the column used to store preference data


182
183
184
# File 'app/models/preference.rb', line 182

def value_attribute
  nature + '_value'
end