Sha256: f9bab82feb7535597a7351e5b37e54b632a6092e7ce4c2319bf6d19cea0fe14a

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'rails/railtie'
require 'active_support/core_ext/object/blank'
require 'active_set'
require 'ostruct'

require 'action_set/version'
require_relative './action_set/instructions/entry_value'
require_relative './action_set/view_helpers'

module ActionSet
  class Railtie < ::Rails::Railtie
    initializer 'action_set.view_helpers' do
      ActiveSupport.on_load :action_view do
        include ViewHelpers
      end
    end
  end

  class Filter < OpenStruct
    def model_name
      OpenStruct.new(param_key: 'filter')
    end
  end

  module ClassMethods
  end

  module InstanceMethods
    def process_set(set)
      @set = set
      @filters = JSON.parse(filter_params.to_json,
                            object_class: Filter)
      active_set = ActiveSet.new(set)
      active_set = active_set.filter(filter_structure) if filter_params.any?
      active_set = active_set.sort(sort_params) if sort_params.any?
      active_set
    end

    private

    def filter_params
      params.fetch(:filter, {}).to_unsafe_hash
    end

    def filter_structure
      filter_params.flatten_keys.reject { |_, v| v.blank? }.each_with_object({}) do |(keypath, value), memo|
        instruction = ActiveSet::Instructions::Entry.new(keypath, value)
        item_with_value = @set.find { |i| !instruction.value_for(item: i).nil? }
        item_value = instruction.value_for(item: item_with_value)
        typecast_value = ActionSet::Instructions::EntryValue.new(value)
                                                            .cast(to: item_value.class)
        memo[keypath] = typecast_value
      end
    end

    def sort_params
      params.fetch(:sort, {}).to_unsafe_hash
    end
  end

  def self.included(receiver)
    receiver.extend         ClassMethods
    receiver.send :include, InstanceMethods
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
actionset-0.1.3 lib/action_set.rb