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-2019 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?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Class Method Details

.[](name) ⇒ Object


114
115
116
# File 'app/models/preference.rb', line 114

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

.check!Object


91
92
93
94
95
# File 'app/models/preference.rb', line 91

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


137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/preference.rb', line 137

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


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/preference.rb', line 120

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


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

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


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/preference.rb', line 157

def set!(name, value, nature = nil)
  name = name.to_s

  preference_ids = Preference.where(name: name).order(:id).offset(1).pluck(:id)
  Preference.where(id: preference_ids).delete_all if preference_ids.any?

  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


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/preference.rb', line 97

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


152
153
154
# File 'app/models/preference.rb', line 152

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


231
232
233
# File 'app/models/preference.rb', line 231

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

#modelObject


236
237
238
# File 'app/models/preference.rb', line 236

def model
  record? ? record_value_type.constantize : nil
end

#set(object) ⇒ Object


220
221
222
223
# File 'app/models/preference.rb', line 220

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

#set!(object) ⇒ Object


225
226
227
228
229
# File 'app/models/preference.rb', line 225

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

#valueObject

Returns basically the value of the preference


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

def value
  send(value_attribute)
end

#value=(object) ⇒ Object


215
216
217
218
# File 'app/models/preference.rb', line 215

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


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

def value_attribute
  nature + '_value'
end