class EasySettings::ParamsWrapper

Attributes

errors[R]

Public Class Methods

from_params(raw_params, project: nil, prefix: nil) click to toggle source
# File lib/redmine_extensions/easy_settings/params_wrapper.rb, line 6
def self.from_params(raw_params, project: nil, prefix: nil)
  case raw_params
  when Hash
    # OK
  when ActionController::Parameters
    raw_params = raw_params.to_unsafe_h
  else
    raw_params = {}
  end

  new(raw_params, project, prefix)
end
new(raw_params, project, prefix) click to toggle source
# File lib/redmine_extensions/easy_settings/params_wrapper.rb, line 19
def initialize(raw_params, project, prefix)
  @raw_params = raw_params
  @project_id = project.is_a?(Project) ? project.id : project
  @prefix = "#{prefix}_" if prefix.present?
  @errors = []

  prepare_params
  prepare_easy_settings
end

Public Instance Methods

save() click to toggle source
# File lib/redmine_extensions/easy_settings/params_wrapper.rb, line 34
def save
  @errors.clear

  @easy_settings.each do |setting|
    # TO CONSIDER: Should this line exist?
    #              This skip callbacks after saving
    #              setting but is it desirable?
    next if !setting.changed?

    if setting.save
      # All good
    else
      @errors << [setting, setting.errors]
    end
  end

  @errors.empty?
end
valid?() click to toggle source
# File lib/redmine_extensions/easy_settings/params_wrapper.rb, line 29
def valid?
  validate
  @errors.empty?
end

Private Instance Methods

prepare_easy_settings() click to toggle source
# File lib/redmine_extensions/easy_settings/params_wrapper.rb, line 62
def prepare_easy_settings
  saved_settings = EasySetting.where(name: @params.keys, project_id: @project_id).map{|e| [e.name, e] }.to_h

  @easy_settings = []
  @params.each do |name, value|
    setting = saved_settings[name]
    setting ||= EasySetting.new(name: name, project_id: @project_id)
    next if setting.disabled_from_params?
    next if value.blank? && setting.skip_blank_params?

    setting.from_params(value)
    @easy_settings << setting
  end
end
prepare_params() click to toggle source
# File lib/redmine_extensions/easy_settings/params_wrapper.rb, line 55
def prepare_params
  @params = {}
  @raw_params.each do |name, value|
    @params["#{@prefix}#{name}"] = value
  end
end
validate() click to toggle source
# File lib/redmine_extensions/easy_settings/params_wrapper.rb, line 77
def validate
  @errors.clear
  @easy_settings.each do |setting|
    if !setting.valid?
      @errors << [setting, setting.errors]
    end
  end
end