module Acts::DataTable::ScopeFilters::ActiveRecord
Public Class Methods
@return [Array<Object>] The actual parameters for a scope call
based on the registered filter and the given arguments. This method should be used to ensure the correct order of arguments when applying a scope.
@example Actual parameter generation for a date range scope
#has_scope_filter :date, :between, [:start_date, :end_date] actual_params(model, :date, :between, {:end_date => '2015-04-01', :start_date => '2015-03-01'}) #=> ['2015-03-01', '2015-04-01']
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 252 def self.actual_params(model, group, scope, args) res = [] self.filter_args(model, group, scope).each do |arg_name| res << args.stringify_keys[arg_name.to_s] end res end
@return [Array<String, Symbol>] the args set up when registering the given filter
@see filter_options for parameters
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 180 def self.filter_args(*args) self.filter_options(*args)[:args] || [] end
@return [Fixnum] the amount of formal parameters the scope is defined with
Note that this will return 0 for no formal parameters which differs to the way ruby 1.8 handles lambda expressions (returning -1)
@see filter_options for parameters
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 191 def self.filter_arity(*args) self.filter_args(*args).size end
@see register_filter for arguments
@return [Hash, NilClass] options given for the chosen filter if available
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 167 def self.filter_options(model, group, scope) res = Acts::DataTable.lookup_nested_hash(self.registered_filters, model.to_s, group.to_s, scope.to_s) unless res raise ArgumentError.new("The scope '#{scope}' was expected to be defined in group '#{group}' of model '#{model}' but couldn't be found.") end res end
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 5 def self.included(base) base.send :extend, ClassMethods end
Checks whether the given argument count matches the given scope filter’s formal parameter count.
@return [TrueClass, FalseClass] true
if the given argument count is
sufficient for the chosen filter.
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 211 def self.matching_arity?(model, group, scope, arg_count) self.filter_arity(model, group, scope) == arg_count end
Registers a filter in the system which is then accessed when applying filters through a scope The filters are kept in this module as it can be accessed from everywhere, while a model class may not be known from within these methods.
@param [ActiveRecord::Base] model
The model class the scope to be added belongs to
@param [String, Symbol] group
The group name the scope belongs to within the +model+
@param [String, Symbol] scope
The scope name, it has to be a valid (named) scope within +model+
@param [Hash] options
Filter options, see #has_scope_filter
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 153 def self.register_filter(model, group, scope, options) unless model.scopes.has_key?(scope.to_sym) raise ArgumentError.new "The scope '#{scope}' in group '#{group}' does not exist in the model class '#{model.to_s}'" end Acts::DataTable.ensure_nested_hash!(self.registered_filters, model.to_s, group.to_s) self.registered_filters[model.to_s][group.to_s][scope.to_s] = options end
@return [TrueClass, FalseClass] true
if the given filter was actually registered before.
@see filter_options for parameters
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 200 def self.registered_filter?(*args) !!(self.filter_options(*args)) end
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 132 def self.registered_filters @@registered_filters ||= {} end
@return [String] The scope filter caption for the given scope @see has_scope_filter for more information about how the caption is generated.
# File lib/acts_as_data_table/scope_filters/active_record.rb, line 219 def self.scope_filter_caption(model, group, scope, args) args ||= {} case caption = self.filter_options(model, group, scope)[:caption] when String caption when Symbol if model.respond_to?(caption) model.send(caption, group, scope, args) else raise ArgumentError.new "The method '#{caption}' was set up as scope filter caption method in model '#{model.name}', but doesn't exist." end when Proc caption.call(args) else options = {:scope => "activerecord.scope_filters.scopes.#{model.name.underscore}"} options.merge!(args.symbolize_keys) I18n.t(scope, options) end end