Module: ActiveQueryable::ClassMethods
- Defined in:
- lib/active_queryable.rb
Overview
Extension methods for ActiveRecord::Base
Instance Method Summary collapse
-
#expand_queryable(options) ⇒ void
A method to expand the filterable keys, useful to allow class inheritance.
-
#of_not(ids) ⇒ ActiveRecord::Relation
Returns a scope with the given ids excluded.
-
#query_by(params) ⇒ ActiveRecord::Relation
Runs a query with the given params.
-
#queryable(options) ⇒ void
Configures the model to be queryable.
- #queryable_scope(params) ⇒ ActiveRecord::Relation
Instance Method Details
#expand_queryable(options) ⇒ void
This method returns an undefined value.
A method to expand the filterable keys, useful to allow class inheritance
91 92 93 94 |
# File 'lib/active_queryable.rb', line 91 def () self. ||= [] self. += ([:filter] || []).map(&:to_sym) end |
#of_not(ids) ⇒ ActiveRecord::Relation
Returns a scope with the given ids excluded
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/active_queryable.rb', line 55 module ClassMethods # Configures the model to be queryable # @example # class User < ApplicationRecord # as_queryable # queryable order: { name: :asc }, page: 1, per: 25, filter: %i[name email] # end # # @param options [Hash] # @option options [Hash<Symbol,Symbol>] :order { id: :asc } # @option options [Integer] :page 1 # @option options [Integer] :per 25 # @option options [Array<Symbol,String>] :filter [] # @return [void] def queryable() () scope :query_by, ->(params) { queryable_scope(params) } scope :of_ids, ->(ids) { where(id: ids) } scope :of_not, ->(ids) { where.not(id: ids) } end # A method to expand the filterable keys, useful to allow class inheritance # @example # class BaseItem < ApplicationRecord # as_queryable # queryable order: { name: :asc }, page: 1, per: 25, filter: %i[name email] # end # class AdvancedItem < BaseItem # expand_queryable filter: %i[phone] # end # # # @param options [Hash] # @option options [Array<Symbol,String>] :filter [] # @return [void] def () self. ||= [] self. += ([:filter] || []).map(&:to_sym) end # @param params [Hash,ActionController::Parameters] # @option params [String] :sort # @option params [Hash] :filter # @option params [String] :page # @option params [String] :per # @option params [Hash] :page # @return [ActiveRecord::Relation] def queryable_scope(params) params = params.to_unsafe_h if params.respond_to? :to_unsafe_h params = params.with_indifferent_access if params.respond_to?(:with_indifferent_access) queryable_log_unpermitted_params(params) order_params = queryable_validate_order_params(params[:sort]) query = queryable_parse_order_scope(order_params, self) queryable_filtered_scope(params, query) end private # @param params [Hash,ActionController::Parameters] # @return [void] def queryable_log_unpermitted_params(params) params.each_key do |k| next if QUERYABLE_VALID_PARAMS.include?(k.to_sym) Rails.logger.debug( "Unsupported key `#{k}` passed to `query_by` will be ignored. Allowed keys: #{QUERYABLE_VALID_PARAMS.join(', ')}" ) end end # @option options [Hash<Symbol,Symbol>] :order { id: :asc } # @option options [Integer] :page 1 # @option options [Integer] :per 25 # @option options [Array<Symbol,String>] :filter [] # @return [void] def () self._queryable_default_order = [:order] || { id: :asc } self._queryable_default_page = [:page] || 1 self._queryable_default_per = [:per] || 25 self._queryable_filter_keys = (([:filter] || []).map(&:to_sym)) end # @param params [Hash,ActionController::Parameters] # @option params [String] :sort # @option params [Hash] :filter # @option params [String] :page # @option params [String] :per # @option params [Hash] :page # @param query [ActiveRecord::Relation] # @return [ActiveRecord::Relation] def queryable_filtered_scope(params, query) filter_params = queryable_validate_filter_params(params[:filter]) page_params = queryable_validate_page_params(params) scope = queryable_parse_filter_scope(filter_params, query) unless page_params[:per] == 'all' scope = scope .page(page_params[:page]) .per(page_params[:per]) end scope end # @param params [String,nil] # @return [Hash] def queryable_validate_order_params(params) queryable_parse_order_params(params) || _queryable_default_order end # @param params [Hash,ActionController::Parameters] # @option params [String] :page # @option params [String] :per # @option params [Hash] :page # @return [Hash] def queryable_validate_page_params(params) page_params = {} if params[:page].respond_to?(:dig) page_params[:page] = params.dig(:page, :number) || _queryable_default_page page_params[:per] = params.dig(:page, :size) || _queryable_default_per else page_params[:page] = params[:page] || _queryable_default_page page_params[:per] = params[:per] || _queryable_default_per end page_params end # @param filter_params [Hash,ActionController::Parameters,nil] # @return [Hash] def queryable_validate_filter_params(filter_params) return nil if filter_params.nil? filters = (((_queryable_filter_keys || []) | (self. || [])) + ['not']).map(&:to_sym) unpermitted = filter_params.except(*filters) Rails.logger.warn("Unpermitted queryable parameters: #{unpermitted.keys.join(', ')}") if unpermitted.present? filter_params.slice(*filters) end # @param params [String,nil] # @return [Hash] def queryable_parse_order_params(params) return nil unless params.is_a? String params.split(',').map! do |param| clean_param = param.start_with?('-') ? param[1..-1] : param [clean_param, clean_param == param ? :asc : :desc] end.to_h end # @param params [Hash,ActionController::Parameters,nil] # @param query [ActiveRecord::Relation] # @return [ActiveRecord::Relation] def queryable_parse_order_scope(params, query) return query unless params params.inject(query) do |current_query, (k, v)| scope = "by_#{k}" if current_query.respond_to?(scope, true) current_query.public_send(scope, v) else current_query.order(params) end end || query end # @param params [Hash,ActionController::Parameters,nil] # @param query [ActiveRecord::Relation] # @return [ActiveRecord::Relation] def queryable_parse_filter_scope(params, query) return query unless params params.inject(query) do |current_query, (k, v)| scope = "of_#{k}" if current_query.respond_to?(scope, true) current_query.public_send(scope, v) else current_query.where(k => v) end end end end |
#query_by(params) ⇒ ActiveRecord::Relation
Runs a query with the given params
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/active_queryable.rb', line 55 module ClassMethods # Configures the model to be queryable # @example # class User < ApplicationRecord # as_queryable # queryable order: { name: :asc }, page: 1, per: 25, filter: %i[name email] # end # # @param options [Hash] # @option options [Hash<Symbol,Symbol>] :order { id: :asc } # @option options [Integer] :page 1 # @option options [Integer] :per 25 # @option options [Array<Symbol,String>] :filter [] # @return [void] def queryable() () scope :query_by, ->(params) { queryable_scope(params) } scope :of_ids, ->(ids) { where(id: ids) } scope :of_not, ->(ids) { where.not(id: ids) } end # A method to expand the filterable keys, useful to allow class inheritance # @example # class BaseItem < ApplicationRecord # as_queryable # queryable order: { name: :asc }, page: 1, per: 25, filter: %i[name email] # end # class AdvancedItem < BaseItem # expand_queryable filter: %i[phone] # end # # # @param options [Hash] # @option options [Array<Symbol,String>] :filter [] # @return [void] def () self. ||= [] self. += ([:filter] || []).map(&:to_sym) end # @param params [Hash,ActionController::Parameters] # @option params [String] :sort # @option params [Hash] :filter # @option params [String] :page # @option params [String] :per # @option params [Hash] :page # @return [ActiveRecord::Relation] def queryable_scope(params) params = params.to_unsafe_h if params.respond_to? :to_unsafe_h params = params.with_indifferent_access if params.respond_to?(:with_indifferent_access) queryable_log_unpermitted_params(params) order_params = queryable_validate_order_params(params[:sort]) query = queryable_parse_order_scope(order_params, self) queryable_filtered_scope(params, query) end private # @param params [Hash,ActionController::Parameters] # @return [void] def queryable_log_unpermitted_params(params) params.each_key do |k| next if QUERYABLE_VALID_PARAMS.include?(k.to_sym) Rails.logger.debug( "Unsupported key `#{k}` passed to `query_by` will be ignored. Allowed keys: #{QUERYABLE_VALID_PARAMS.join(', ')}" ) end end # @option options [Hash<Symbol,Symbol>] :order { id: :asc } # @option options [Integer] :page 1 # @option options [Integer] :per 25 # @option options [Array<Symbol,String>] :filter [] # @return [void] def () self._queryable_default_order = [:order] || { id: :asc } self._queryable_default_page = [:page] || 1 self._queryable_default_per = [:per] || 25 self._queryable_filter_keys = (([:filter] || []).map(&:to_sym)) end # @param params [Hash,ActionController::Parameters] # @option params [String] :sort # @option params [Hash] :filter # @option params [String] :page # @option params [String] :per # @option params [Hash] :page # @param query [ActiveRecord::Relation] # @return [ActiveRecord::Relation] def queryable_filtered_scope(params, query) filter_params = queryable_validate_filter_params(params[:filter]) page_params = queryable_validate_page_params(params) scope = queryable_parse_filter_scope(filter_params, query) unless page_params[:per] == 'all' scope = scope .page(page_params[:page]) .per(page_params[:per]) end scope end # @param params [String,nil] # @return [Hash] def queryable_validate_order_params(params) queryable_parse_order_params(params) || _queryable_default_order end # @param params [Hash,ActionController::Parameters] # @option params [String] :page # @option params [String] :per # @option params [Hash] :page # @return [Hash] def queryable_validate_page_params(params) page_params = {} if params[:page].respond_to?(:dig) page_params[:page] = params.dig(:page, :number) || _queryable_default_page page_params[:per] = params.dig(:page, :size) || _queryable_default_per else page_params[:page] = params[:page] || _queryable_default_page page_params[:per] = params[:per] || _queryable_default_per end page_params end # @param filter_params [Hash,ActionController::Parameters,nil] # @return [Hash] def queryable_validate_filter_params(filter_params) return nil if filter_params.nil? filters = (((_queryable_filter_keys || []) | (self. || [])) + ['not']).map(&:to_sym) unpermitted = filter_params.except(*filters) Rails.logger.warn("Unpermitted queryable parameters: #{unpermitted.keys.join(', ')}") if unpermitted.present? filter_params.slice(*filters) end # @param params [String,nil] # @return [Hash] def queryable_parse_order_params(params) return nil unless params.is_a? String params.split(',').map! do |param| clean_param = param.start_with?('-') ? param[1..-1] : param [clean_param, clean_param == param ? :asc : :desc] end.to_h end # @param params [Hash,ActionController::Parameters,nil] # @param query [ActiveRecord::Relation] # @return [ActiveRecord::Relation] def queryable_parse_order_scope(params, query) return query unless params params.inject(query) do |current_query, (k, v)| scope = "by_#{k}" if current_query.respond_to?(scope, true) current_query.public_send(scope, v) else current_query.order(params) end end || query end # @param params [Hash,ActionController::Parameters,nil] # @param query [ActiveRecord::Relation] # @return [ActiveRecord::Relation] def queryable_parse_filter_scope(params, query) return query unless params params.inject(query) do |current_query, (k, v)| scope = "of_#{k}" if current_query.respond_to?(scope, true) current_query.public_send(scope, v) else current_query.where(k => v) end end end end |
#queryable(options) ⇒ void
This method returns an undefined value.
Configures the model to be queryable
69 70 71 72 73 74 75 |
# File 'lib/active_queryable.rb', line 69 def queryable() () scope :query_by, ->(params) { queryable_scope(params) } scope :of_ids, ->(ids) { where(id: ids) } scope :of_not, ->(ids) { where.not(id: ids) } end |
#queryable_scope(params) ⇒ ActiveRecord::Relation
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_queryable.rb', line 103 def queryable_scope(params) params = params.to_unsafe_h if params.respond_to? :to_unsafe_h params = params.with_indifferent_access if params.respond_to?(:with_indifferent_access) queryable_log_unpermitted_params(params) order_params = queryable_validate_order_params(params[:sort]) query = queryable_parse_order_scope(order_params, self) queryable_filtered_scope(params, query) end |