Class: MightyGrid::Base
- Inherits:
-
Object
- Object
- MightyGrid::Base
- Includes:
- Filters, Parameters
- Defined in:
- lib/mighty_grid/base.rb
Class Attribute Summary collapse
-
.klass ⇒ Object
readonly
Returns the value of attribute klass.
-
.relation ⇒ Object
readonly
Returns the value of attribute relation.
-
.sphinx_options(options = {}) ⇒ Object
readonly
Returns the value of attribute sphinx_options.
-
.use_sphinx ⇒ Object
readonly
Returns the value of attribute use_sphinx.
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#mg_params ⇒ Object
readonly
Returns the value of attribute mg_params.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output_buffer ⇒ Object
Returns the value of attribute output_buffer.
-
#params ⇒ Object
readonly
Get controller parameters.
-
#query ⇒ Object
Returns the value of attribute query.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
-
#sphinx_options ⇒ Object
readonly
Returns the value of attribute sphinx_options.
-
#use_sphinx ⇒ Object
readonly
Returns the value of attribute use_sphinx.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(params, opts = {}) ⇒ Base
constructor
:nodoc:.
-
#like_operator ⇒ Object
Get
like
orilike
operator depending on the database adapter. - #read ⇒ Object
Methods included from Parameters
Methods included from Filters
Constructor Details
#initialize(params, opts = {}) ⇒ Base
:nodoc:
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mighty_grid/base.rb', line 9 def initialize(params, opts = {}) #:nodoc: @controller_params = params # Get active controller through params if controller = "#{params[:controller].camelize}Controller".safe_constantize @controller = ObjectSpace.each_object(controller).first end @filters = self.class.filters.dup @query = self.class.try(:query).try(:dup) @use_sphinx = self.class.use_sphinx || false @sphinx_options = {} @options = { page: 1, per_page: MightyGrid.per_page, name: MightyGrid.grid_name, include: nil, joins: nil, conditions: nil, group: nil, order: nil } opts.assert_valid_keys(@options.keys) @options.merge!(opts) @klass = self.class.klass @relation = self.class.relation @relation = yield(@relation) if block_given? @name = @options[:name].to_s load_grid_params end |
Class Attribute Details
.klass ⇒ Object (readonly)
Returns the value of attribute klass.
47 48 49 |
# File 'lib/mighty_grid/base.rb', line 47 def klass @klass end |
.relation ⇒ Object (readonly)
Returns the value of attribute relation.
47 48 49 |
# File 'lib/mighty_grid/base.rb', line 47 def relation @relation end |
.sphinx_options(options = {}) ⇒ Object (readonly)
Returns the value of attribute sphinx_options.
47 48 49 |
# File 'lib/mighty_grid/base.rb', line 47 def @sphinx_options end |
.use_sphinx ⇒ Object (readonly)
Returns the value of attribute use_sphinx.
47 48 49 |
# File 'lib/mighty_grid/base.rb', line 47 def use_sphinx @use_sphinx end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def controller @controller end |
#filters ⇒ Object
Returns the value of attribute filters.
7 8 9 |
# File 'lib/mighty_grid/base.rb', line 7 def filters @filters end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def klass @klass end |
#mg_params ⇒ Object (readonly)
Returns the value of attribute mg_params.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def mg_params @mg_params end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def @options end |
#output_buffer ⇒ Object
Returns the value of attribute output_buffer.
7 8 9 |
# File 'lib/mighty_grid/base.rb', line 7 def output_buffer @output_buffer end |
#params ⇒ Object (readonly)
Get controller parameters
100 101 102 |
# File 'lib/mighty_grid/base.rb', line 100 def params @params end |
#query ⇒ Object
Returns the value of attribute query.
7 8 9 |
# File 'lib/mighty_grid/base.rb', line 7 def query @query end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def relation @relation end |
#sphinx_options ⇒ Object (readonly)
Returns the value of attribute sphinx_options.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def @sphinx_options end |
#use_sphinx ⇒ Object (readonly)
Returns the value of attribute use_sphinx.
6 7 8 |
# File 'lib/mighty_grid/base.rb', line 6 def use_sphinx @use_sphinx end |
Class Method Details
.scope(&block) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/mighty_grid/base.rb', line 49 def scope(&block) if block_given? klass_or_relation = yield @relation = klass_or_relation @klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation end end |
.use_thinking_sphinx(bool = false) ⇒ Object
57 58 59 60 |
# File 'lib/mighty_grid/base.rb', line 57 def use_thinking_sphinx(bool=false) fail MightyGrid::Exceptions::ArgumentError.new('Parameter should have type Boolean: true or false') unless bool.in? [true, false] @use_sphinx = bool end |
Instance Method Details
#like_operator ⇒ Object
Get like
or ilike
operator depending on the database adapter
105 106 107 108 109 110 111 |
# File 'lib/mighty_grid/base.rb', line 105 def like_operator if ActiveRecord::ConnectionAdapters.const_defined?(:PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) 'ILIKE' else 'LIKE' end end |
#read ⇒ Object
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 |
# File 'lib/mighty_grid/base.rb', line 67 def read search_apply_filter if @use_sphinx if @use_sphinx && @query.present? ts_apply_filters if @mg_params[:order].present? && current_order_direction.present? && !@mg_params[:order].kind_of?(Hash) @sphinx_options.merge!(order: "#{@mg_params[:order]} #{current_order_direction.to_sym}") else @sphinx_options.merge!(order: @mg_params[:order]) end @relation = @klass.search(ThinkingSphinx::Query.escape(@query), @sphinx_options) @relation = @relation .page(@mg_params[:page]) .per(@mg_params[:per_page]) else ar_apply_filters if @mg_params[:order].present? && current_order_direction.present? && !@mg_params[:order].kind_of?(Hash) @relation = @relation.order("#{@mg_params[:order]} #{current_order_direction.to_sym}") else @relation = @relation.order(@mg_params[:order]) end @relation = @relation .page(@mg_params[:page]) .per(@mg_params[:per_page]) .includes(@options[:include]) .joins(@options[:joins]) .where(@options[:conditions]) .group(@options[:group]) end end |