Class: EasySetting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/easy_setting.rb

Overview

EasySetting

Mapping

type

boolean, integer, float (default: none)

default

(default: nil)

disabled_from_params

(default: false)

skip_blank_params

(default: false)

from_params

Accept block or lambda with one argument for raw_value. Evaluated in EasySetting instance. (default: none)

validate

Accept block or lambda. Evaluated in EasySetting instance. (default: none)

after_save

Accept block or lambda. Evaluated in EasySetting instance. (default: none)

Mapping examples

EasySetting.map do

  # Integer key
  key :integer_key do
    type 'integer'
    default 42

    validate { errors.add(:value, 'Bad range') if value < 0 || value > 500 }
    after_save { Mailer.integer_key_changed }
  end

  # Custom definition
  key :custom_key do
    default 'Ondra'
    from_params lambda {|v| v.to_s }
  end

  # Cannot be changed via params
  key :not_from_params do
    default 'Ondra'
    disabled_from_params
  end

end

# Boolean key definition
EasySetting.map.key(:boolean_key, type: 'boolean')

# Multiple defintions
EasySetting.map.keys(:key1, :key2, :key3, disabled_from_params: true)

Constant Summary collapse

@@mapper =
EasySettings::Mapper.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.beginning_of_fiscal_year(date = nil) ⇒ Object

TODO: Move away



213
214
215
216
217
218
219
220
221
222
# File 'app/models/easy_setting.rb', line 213

def self.beginning_of_fiscal_year(date = nil)
  today = date || Date.today
  fy = get_beginning_of_fiscal_for_year(today.year)

  if fy <= today
    fy
  else
    fy - 1.year
  end
end

.boolean_keysObject



117
118
119
# File 'app/models/easy_setting.rb', line 117

def self.boolean_keys
  []
end

.copy_all_project_settings(source_project, target_project) ⇒ Object



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

def self.copy_all_project_settings(source_project, target_project)
  source_project = source_project.id if source_project.is_a?(Project)
  target_project = target_project.id if target_project.is_a?(Project)

  options = { scope: EasySetting.where(project_id: source_project) }
  Redmine::Hook.call_hook(:copy_all_project_settings_exceceptions, options: options)
  source_project_names = options[:scope].pluck(:name)

  source_project_names.each do |name|
    copy_project_settings(name, source_project, target_project)
  end
end

.copy_project_settings(setting_name, source_project_id, target_project_id) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/easy_setting.rb', line 134

def self.copy_project_settings(setting_name, source_project_id, target_project_id)
  source = EasySetting.where(name: setting_name, project_id: source_project_id).first
  target = EasySetting.where(name: setting_name, project_id: target_project_id).first

  if source.nil? && !target.nil?
    target.destroy
  elsif !source.nil? && target.nil?
    EasySetting.create(name: setting_name, project_id: target_project_id, value: source.value)
  elsif !source.nil? && !target.nil? && target.value != source.value
    target.value = source.value
    target.save
  end
end

.delete_key(key, project) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/easy_setting.rb', line 186

def self.delete_key(key, project)
  if project.is_a?(Project)
    project_id = project.id
  elsif project.present?
    project_id = project.to_i
  else
    project_id = nil
  end

  return if project_id.nil?
  EasySetting.where(name: key, project_id: project_id).destroy_all
end

.end_of_fiscal_year(date = nil) ⇒ Object

TODO: Move away



225
226
227
# File 'app/models/easy_setting.rb', line 225

def self.end_of_fiscal_year(date = nil)
  beginning_of_fiscal_year(date) + 1.year - 1.day
end

.get_beginning_of_fiscal_for_year(year = nil) ⇒ Object

TODO: Move away



200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/easy_setting.rb', line 200

def self.get_beginning_of_fiscal_for_year(year = nil)
  f_y = year || Date.today.year
  f_m = (EasySetting.value('fiscal_month') || 1).to_i
  f_d = 1

  begin
    Date.civil(f_y, f_m, f_d)
  rescue
    Date.civil(f_y, 1, 1)
  end
end

.map(&block) ⇒ Object



80
81
82
83
84
85
86
# File 'app/models/easy_setting.rb', line 80

def self.map(&block)
  if block
    @@mapper.instance_eval(&block)
  else
    @@mapper
  end
end

.mapper_clear_cachesObject



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

def self.mapper_clear_caches
  @mapper_defaults = nil
end

.mapper_defaultsObject



88
89
90
# File 'app/models/easy_setting.rb', line 88

def self.mapper_defaults
  @mapper_defaults ||= @@mapper.default_values
end

.plugin_defaultsObject



148
149
150
151
152
153
154
155
156
157
# File 'app/models/easy_setting.rb', line 148

def self.plugin_defaults
  @plugin_defaults ||= Redmine::Plugin.all.inject({}) do |res, p|
    if p.settings && p.settings[:easy_settings].is_a?(Hash)
      p.settings[:easy_settings].each do |key, value|
        res["#{p.id}_#{key}"] = value
      end
    end
    res
  end
end

.value(key, project = nil, use_fallback = true) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/easy_setting.rb', line 159

def self.value(key, project = nil, use_fallback = true)
  if project.is_a?(Project)
    project_id = project.id
  elsif project.present?
    project_id = project.to_i
  else
    project_id = nil
  end

  cache_key = "EasySetting/#{key}/#{project_id}"
  fallback_cache_key = "EasySetting/#{key}/"

  result = Rails.cache.fetch(cache_key) do
    EasySetting.where(name: key, project_id: project_id).limit(1).pluck(:value).first
  end

  if use_fallback && result.nil?
    result = Rails.cache.fetch(fallback_cache_key) do
      EasySetting.where(name: key, project_id: nil).limit(1).pluck(:value).first
    end
  end

  result = plugin_defaults[key.to_s] if result.nil?
  result = mapper_defaults[key.to_s] if result.nil?
  result
end

Instance Method Details

#disabled_from_params?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/easy_setting.rb', line 109

def disabled_from_params?
  @@mapper.disabled_from_params?(self)
end

#from_params(raw_value) ⇒ Object



104
105
106
107
# File 'app/models/easy_setting.rb', line 104

def from_params(raw_value)
  return if disabled_from_params?
  self.value = @@mapper.from_params(self, raw_value)
end

#mapper_after_saveObject



96
97
98
# File 'app/models/easy_setting.rb', line 96

def mapper_after_save
  @@mapper.after_save(self)
end

#mapper_validateObject



100
101
102
# File 'app/models/easy_setting.rb', line 100

def mapper_validate
  @@mapper.validate(self)
end

#skip_blank_params?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/easy_setting.rb', line 113

def skip_blank_params?
  @@mapper.skip_blank_params?(self)
end