Sha256: fcbfa9c659cc9e78e8f566ac365c0289c5394aa19c9e816093c68ecb5f90bd18

Contents?: true

Size: 1.77 KB

Versions: 32

Compression:

Stored size: 1.77 KB

Contents

module PushType
  module Filterable
    extend ActiveSupport::Concern

    private

    def filter_scope
      self.class.filter_scope
    end

    def filter_obj
      instance_variable_get "@#{ filter_scope }"
    end

    def filter_obj_type
      filter_obj.type.underscore.to_sym
    end

    def before_load_filters
      filters = self.send("before_#{ filter_scope }_load_filters")
      filters.each { |f| run_node_hook(*f) } unless filters.blank?
    end

    def around_action_filters
      before_filters = self.send("before_#{ filter_scope }_action_filters")
      before_filters.each { |f| run_node_hook(*f) } unless before_filters.blank?
      yield
      after_filters = self.send("after_#{ filter_scope }_action_filters")
      after_filters.each { |f| run_node_hook(*f) } unless after_filters.blank?
    end

    def run_node_hook(*args)
      methods_or_proc, opts = case args.last
        when Proc then args.first.is_a?(Hash) ? [ args.last, args.first ] : [ args.last, {} ]
        when Hash then [ args[0..-2], args.last ]
        else [ args, {} ]
      end

      unless filter_obj_not_in(opts[:only]) || filter_obj_in(opts[:except])
        if methods_or_proc.respond_to?(:call)
          instance_exec(&methods_or_proc)
        else
          Array(methods_or_proc).each { |m| send(m) }
        end
      end
    end

    def filter_obj_in(types)
      types && filter_obj && Array(types).include?(filter_obj_type)
    end

    def filter_obj_not_in(types)
      types && filter_obj && !Array(types).include?(filter_obj_type)
    end

    module ClassMethods

      attr_reader :filter_scope

      def hooks_for(sym, opts)
        @filter_scope = sym
        prepend_before_action :before_load_filters, opts
        around_action :around_action_filters, opts
      end

    end

  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
push_type_core-0.9.5 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.9.3 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.9.2 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.9.1 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.9.0 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.9.0.beta.4 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.9.0.beta.3 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.9.0.beta.2 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.8.2 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.8.1 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.8.0 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.8.0.beta.3 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.8.0.beta.2 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.8.0.beta.1 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.7.0 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.7.0.beta.1 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.6.0 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.6.0.beta.4 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.6.0.beta.3 app/controllers/concerns/push_type/filterable.rb
push_type_core-0.6.0.beta.2 app/controllers/concerns/push_type/filterable.rb