Sha256: 8cec489e83aaee5a0180ac10724559af911434ded354a4664d4e2c3cc443c954

Contents?: true

Size: 1.98 KB

Versions: 27

Compression:

Stored size: 1.98 KB

Contents

module EasySettings
  class ParamsWrapper

    attr_reader :errors

    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

    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

    def valid?
      validate
      @errors.empty?
    end

    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

    private

      def prepare_params
        @params = {}
        @raw_params.each do |name, value|
          @params["#{@prefix}#{name}"] = value
        end
      end

      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

      def validate
        @errors.clear
        @easy_settings.each do |setting|
          if !setting.valid?
            @errors << [setting, setting.errors]
          end
        end
      end

  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
redmine_extensions-1.2.0 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-1.1.2 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-1.1.1 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-1.1.0 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-1.0.0 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.6.3 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.6.2 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.6.1 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.6.0 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.4.5 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.5.1 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.4.4 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.4.3 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.4.1 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.4.0 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.4.0.beta lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.3.11 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.3.10 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.3.9 lib/redmine_extensions/easy_settings/params_wrapper.rb
redmine_extensions-0.3.8 lib/redmine_extensions/easy_settings/params_wrapper.rb