mighty_grid 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 882bea1ae17754a079753be41bbeb7a855b03956
4
- data.tar.gz: 44283d60dde96275f986160defedf980b3322996
3
+ metadata.gz: c8d3b69949a5bd99efae0e4cdb2155b40c4f56e2
4
+ data.tar.gz: 4d619b1988d418b5d784bd518674af8c3f43be9b
5
5
  SHA512:
6
- metadata.gz: 9d6444d5e8002af7eb9c30c33873e6174f519ee7da1dfea64792563ac8bdf0efcb76d77df08710f46be319f7c803205ad77c3c6b9af421eea63e8cbecd861299
7
- data.tar.gz: d8faca0136abe01cb09ee4aea111d4a1457553f96c93482624fa3dc9f9fbec1d2f545dd0588a871b88e2bf315d962b8729ad2c127faa78f65122b575411f6b99
6
+ metadata.gz: 6a887f14228bb0c6659cce9d4c1a043f722ebd4632a56ab1f1d7643c30efbe2be88c35c92712b5f8a5474fb85a5844b83949a5c1a92d6b013980333bb7e4a08f
7
+ data.tar.gz: 705d021a01263bfe8008168b5e65c047e3cdae352743c2b7e39189248d3aaf24390361f2847d9a25143b78788ba7f0bcc3ca0d5d4804b437a34b84951aa0754d
@@ -3,8 +3,8 @@ module MightyGrid
3
3
  include MightyGrid::Filters
4
4
  include MightyGrid::Parameters
5
5
 
6
- attr_reader :klass, :name, :relation, :options, :mg_params, :params, :controller
7
- attr_accessor :output_buffer, :filters
6
+ attr_reader :klass, :name, :relation, :options, :mg_params, :params, :controller, :use_sphinx, :sphinx_options
7
+ attr_accessor :output_buffer, :filters, :query
8
8
 
9
9
  def initialize(params, opts = {}) #:nodoc:
10
10
  @controller_params = params
@@ -15,6 +15,9 @@ module MightyGrid
15
15
  end
16
16
 
17
17
  @filters = self.class.filters.dup
18
+ @query = self.class.try(:query).try(:dup)
19
+ @use_sphinx = self.class.use_sphinx || false
20
+ @sphinx_options = {}
18
21
 
19
22
  @options = {
20
23
  page: 1,
@@ -41,7 +44,7 @@ module MightyGrid
41
44
  end
42
45
 
43
46
  class << self
44
- attr_reader :klass, :relation
47
+ attr_reader :klass, :relation, :use_sphinx, :sphinx_options
45
48
 
46
49
  def scope(&block)
47
50
  if block_given?
@@ -50,23 +53,45 @@ module MightyGrid
50
53
  @klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation
51
54
  end
52
55
  end
56
+
57
+ def use_thinking_sphinx(bool=false)
58
+ fail MightyGrid::Exceptions::ArgumentError.new('Parameter should have type Boolean: true or false') unless bool.in? [true, false]
59
+ @use_sphinx = bool
60
+ end
61
+
62
+ def sphinx_options(options = {})
63
+ @sphinx_options = options
64
+ end
53
65
  end
54
66
 
55
67
  def read
56
- apply_filters
57
- if @mg_params[:order].present? && current_order_direction.present? && !@mg_params[:order].kind_of?(Hash)
58
- @relation = @relation.order("#{@mg_params[:order]} #{current_order_direction.to_sym}")
68
+ search_apply_filter if @use_sphinx
69
+
70
+ if @use_sphinx && @query
71
+ ts_apply_filters
72
+ if @mg_params[:order].present? && current_order_direction.present? && !@mg_params[:order].kind_of?(Hash)
73
+ @sphinx_options.merge!(order: "#{@mg_params[:order]} #{current_order_direction.to_sym}")
74
+ else
75
+ @sphinx_options.merge!(order: @mg_params[:order])
76
+ end
77
+
78
+ @relation = @klass.search(ThinkingSphinx::Query.escape(@query), @sphinx_options)
59
79
  else
60
- @relation = @relation.order(@mg_params[:order])
61
- end
80
+ ar_apply_filters
81
+ if @mg_params[:order].present? && current_order_direction.present? && !@mg_params[:order].kind_of?(Hash)
82
+ @relation = @relation.order("#{@mg_params[:order]} #{current_order_direction.to_sym}")
83
+ else
84
+ @relation = @relation.order(@mg_params[:order])
85
+ end
62
86
 
63
- @relation = @relation
64
- .page(@mg_params[:page])
65
- .per(@mg_params[:per_page])
66
- .includes(@options[:include])
67
- .joins(@options[:joins])
68
- .where(@options[:conditions])
69
- .group(@options[:group])
87
+ @relation = @relation
88
+ .page(@mg_params[:page])
89
+ .per(@mg_params[:per_page])
90
+ .includes(@options[:include])
91
+ .joins(@options[:joins])
92
+ .where(@options[:conditions])
93
+ .group(@options[:group])
94
+ end
70
95
  end
71
96
 
72
97
  # Get controller parameters
@@ -1,6 +1,7 @@
1
1
  module MightyGrid
2
2
  class FilterRenderer
3
3
  include ActionView::Helpers
4
+ include ActionView::Context
4
5
 
5
6
  def initialize(grid, view)
6
7
  @grid = grid
@@ -43,17 +44,26 @@ module MightyGrid
43
44
  end
44
45
 
45
46
  # Get button for Apply filter changes
46
- def submit(content = nil, options = {})
47
+ def submit(content = nil, options = {}, &block)
47
48
  content = I18n.t('mighty_grid.filters.submit', default: 'Apply changes') if content.blank?
48
49
  options.merge!(type: :submit)
49
- content_tag(:button, content, options)
50
+ if block_given?
51
+ content_tag(:button, nil, options, &block)
52
+ else
53
+ content_tag(:button, content, options)
54
+ end
55
+
50
56
  end
51
57
 
52
58
  # Get button for Reset filter changes
53
- def reset(content = nil, options = {})
59
+ def reset(content = nil, options = {}, &block)
54
60
  content = I18n.t('mighty_grid.filters.reset', default: 'Reset changes') if content.blank?
55
61
  options.merge!(type: :reset)
56
- content_tag(:button, content, options)
62
+ if block_given?
63
+ content_tag(:button, nil, options, &block)
64
+ else
65
+ content_tag(:button, content, options)
66
+ end
57
67
  end
58
68
 
59
69
  private
@@ -2,6 +2,7 @@ module MightyGrid
2
2
  module Filters
3
3
  class Base
4
4
  attr_reader :options, :attribute, :model, :default
5
+ attr_accessor :search_value
5
6
 
6
7
  class_attribute :default_options
7
8
  self.default_options = {
@@ -0,0 +1,6 @@
1
+ module MightyGrid
2
+ module Filters
3
+ class SearchFilter < Base
4
+ end
5
+ end
6
+ end
@@ -11,11 +11,16 @@ module MightyGrid
11
11
  autoload :EnumFilter
12
12
  autoload :BooleanFilter
13
13
  autoload :CustomFilter
14
+ autoload :SearchFilter
14
15
 
15
16
  def self.included(base)
16
17
  base.class_eval do
17
18
  class_attribute :filters
19
+ class_attribute :query
20
+ class_attribute :search_name
18
21
  self.filters = {}
22
+ self.query = nil
23
+ self.search_name = nil
19
24
 
20
25
  extend MapType
21
26
  map_type :string, to: MightyGrid::Filters::StringFilter
@@ -23,6 +28,7 @@ module MightyGrid
23
28
  map_type :enum, to: MightyGrid::Filters::EnumFilter
24
29
  map_type :boolean, to: MightyGrid::Filters::BooleanFilter
25
30
  map_type :custom, to: MightyGrid::Filters::CustomFilter
31
+ map_type :search, to: MightyGrid::Filters::SearchFilter
26
32
  end
27
33
 
28
34
  base.send :extend, ClassMethods
@@ -51,18 +57,25 @@ module MightyGrid
51
57
  "#{name}[#{filter_param_name}][#{field_name}]"
52
58
  end
53
59
 
60
+ def get_search_name
61
+ self.search_name
62
+ end
63
+
64
+ def get_filter_value(filter_name, filter)
65
+ if filter_params.present?
66
+ filter_params[filter_name.to_s]
67
+ else
68
+ filter.default.to_s
69
+ end
70
+ end
71
+
54
72
  # Apply filters
55
- def apply_filters
73
+ def ar_apply_filters
56
74
  @filters.each_pair do |filter_name, filter|
57
75
  next if (filter_params.blank? && filter.default.blank? ||
58
76
  filter_params.present? && filter_params[filter_name.to_s].blank?)
59
77
 
60
- filter_value =
61
- if filter_params.present?
62
- filter_params[filter_name.to_s]
63
- else
64
- filter.default.to_s
65
- end
78
+ filter_value = get_filter_value(filter_name, filter)
66
79
 
67
80
  table_name = filter.model.table_name
68
81
 
@@ -83,6 +96,15 @@ module MightyGrid
83
96
  end
84
97
  end
85
98
  end
99
+
100
+ # Thinking Sphinx apply filters
101
+ def ts_apply_filters
102
+ # TODO: Make filters for Thinking Sphinx
103
+ end
104
+
105
+ def search_apply_filter
106
+ self.query = get_filter_value(self.search_name, @filters[self.search_name])
107
+ end
86
108
  end
87
109
 
88
110
  module ClassMethods
@@ -102,11 +124,17 @@ module MightyGrid
102
124
  end
103
125
  end
104
126
 
127
+ def search(name)
128
+ filter(name, :search)
129
+ self.search_name = name
130
+ end
131
+
105
132
  protected
106
133
 
107
134
  def inherited(child_class)
108
135
  super(child_class)
109
136
  child_class.filters = self.filters.clone
137
+ child_class.query = self.query.try(:clone)
110
138
  end
111
139
  end
112
140
  end
@@ -13,7 +13,11 @@ module MightyGrid
13
13
  if current_grid_params
14
14
  @mg_params.merge!(current_grid_params.symbolize_keys)
15
15
  if @mg_params[:order].present? && !@mg_params[:order].to_s.include?('.') && !@mg_params[:order].kind_of?(Hash)
16
- @mg_params[:order] = "#{klass.table_name}.#{@mg_params[:order]}"
16
+ if @use_sphinx
17
+ @mg_params[:order] = @mg_params[:order].to_s
18
+ else
19
+ @mg_params[:order] = "#{klass.table_name}.#{@mg_params[:order]}"
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -5,7 +5,7 @@ module MightyGrid
5
5
 
6
6
  module VERSION
7
7
  MAJOR = 2
8
- MINOR = 0
8
+ MINOR = 1
9
9
  TINY = 0
10
10
  PRE = nil
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mighty_grid
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jurrick
@@ -127,6 +127,7 @@ files:
127
127
  - lib/mighty_grid/filters/boolean_filter.rb
128
128
  - lib/mighty_grid/filters/custom_filter.rb
129
129
  - lib/mighty_grid/filters/enum_filter.rb
130
+ - lib/mighty_grid/filters/search_filter.rb
130
131
  - lib/mighty_grid/filters/string_filter.rb
131
132
  - lib/mighty_grid/filters/text_filter.rb
132
133
  - lib/mighty_grid/grid_renderer.rb