mighty_grid 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b767e8e8c424d0fd0b2aa47ef21df2228781185
4
- data.tar.gz: 6d6db95c86982b32d8bb99b3f75a6e6dbacada99
3
+ metadata.gz: db143f79e6b1f6002945861e8da4884867dda3b8
4
+ data.tar.gz: c2164ac042c84c00515e03dd41dcb5ddddd3c2f3
5
5
  SHA512:
6
- metadata.gz: d95807732eb29bdcd4f6261e0ee5d0e0d52490f548455464f62806846267913a1faf70866fa668d2ad2afbaa0178576d06c6f453d9c98f0e062caf229b915408
7
- data.tar.gz: 9ee5f55ba15eabbde84ef511a5f170d31f50d0af2a8f73c8ab6639f9a57a2b6cfcd5e1eabebaf08238f102f43c719284a86e5693f59f3771e51af01e6a572083
6
+ metadata.gz: 8a36b57f6971bb4e3437555f56cccfe0dde9349d5cf5ef809ebcb29dd6db09899ab85e23b78d1fc4ecc7c5aa21bb20f3e2540318de0b78b52db2eb9b8c00ee18
7
+ data.tar.gz: 753d836d4737f542a101362b177c6d0762e260a9851b868ecc04bc830c22594e70d3df890428ffe1c8683b65ee2f3d83eb02ba33ab64a67cbc9b8bb364e14430
data/README.md CHANGED
@@ -21,6 +21,7 @@ Or install it yourself as:
21
21
  ### Controller
22
22
 
23
23
  You can define class or relation in <tt>init_grid</tt> method.
24
+
24
25
  ```
25
26
  def index
26
27
  @products_grid = init_grid(Product)
@@ -42,6 +43,7 @@ end
42
43
  ### General configuration options
43
44
 
44
45
  You can configure the following default values by overriding these values using <tt>MightyGrid.configure</tt> method.
46
+
45
47
  ```
46
48
  per_page # 15 by default
47
49
  order_direction # 'asc' by default
@@ -11,11 +11,13 @@ module MightyGrid
11
11
  @filters = {}
12
12
 
13
13
  @options = {
14
- :page => 1,
15
- :per_page => MightyGrid.config.per_page,
16
- :name => MightyGrid.config.grid_name,
17
- :include => nil,
18
- :joins => nil,
14
+ :page => 1,
15
+ :per_page => MightyGrid.config.per_page,
16
+ :name => MightyGrid.config.grid_name,
17
+ :include => nil,
18
+ :joins => nil,
19
+ :conditions => nil,
20
+ :group => nil
19
21
  }
20
22
 
21
23
  opts.assert_valid_keys(@options.keys)
@@ -23,37 +25,54 @@ module MightyGrid
23
25
 
24
26
  @name = @options[:name].to_s
25
27
 
26
- load_grid_params
27
-
28
28
  @relation = klass_or_relation
29
29
 
30
30
  @klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation
31
31
 
32
+ load_grid_params
32
33
  end
33
34
 
34
35
  def read
35
36
  apply_filters
36
- @relation = @relation.order(@mg_params[:order] => current_order_direction.to_sym) if @mg_params[:order].present? && current_order_direction.present?
37
+ @relation = @relation.order("#{@mg_params[:order]} #{current_order_direction.to_sym}") if @mg_params[:order].present? && current_order_direction.present?
37
38
  @relation = @relation.
38
39
  page(@mg_params[:page]).
39
40
  per(@mg_params[:per_page]).
40
41
  includes(@options[:include]).
41
- joins(@options[:joins])
42
+ joins(@options[:joins]).
43
+ where(@options[:conditions]).
44
+ group(@options[:group])
42
45
  end
43
46
 
44
47
  # Apply filters
45
48
  def apply_filters
46
49
  filter_params.each do |filter_name, filter_value|
47
- next if filter_value.blank? || !klass.column_names.include?(filter_name)
48
- field_type = klass.columns_hash[filter_name].type
50
+ name, table_name = filter_name.split('.').reverse
51
+
52
+ if table_name && Object.const_defined?(table_name.classify)
53
+ model = table_name.classify.constantize
54
+ else
55
+ model = klass
56
+ end
57
+
58
+ next if filter_value.blank? || !model.column_names.include?(name)
59
+
60
+ if model && model.superclass == ActiveRecord::Base
61
+ filter_type = model.columns_hash[name].type
62
+ else
63
+ next
64
+ end
65
+
66
+ field_type ||= model.columns_hash[name].type
67
+ table_name = model.table_name
49
68
 
50
69
  if @filters.has_key?(filter_name.to_sym) && @filters[filter_name.to_sym].is_a?(Array)
51
- @relation = @relation.where(filter_name => filter_value)
70
+ @relation = @relation.where(table_name => { filter_name => filter_value })
52
71
  elsif field_type == :boolean
53
72
  value = ['true', '1', 't'].include?(filter_value) ? true : false
54
- @relation = @relation.where(filter_name => value)
73
+ @relation = @relation.where(table_name => {filter_name => value})
55
74
  elsif [:string, :text].include?(field_type)
56
- @relation = @relation.where("#{klass.table_name}.#{filter_name} #{like_operator} ?", "%#{filter_value}%")
75
+ @relation = @relation.where("#{table_name}.#{name} #{like_operator} ?", "%#{filter_value}%")
57
76
  end
58
77
  end
59
78
  end
@@ -69,6 +88,9 @@ module MightyGrid
69
88
  @mg_params.merge!(@options)
70
89
  if current_grid_params
71
90
  @mg_params.merge!(current_grid_params.symbolize_keys)
91
+ if @mg_params[:order].present? && !@mg_params[:order].to_s.include?('.')
92
+ @mg_params[:order] = "#{klass.table_name}.#{@mg_params[:order]}"
93
+ end
72
94
  end
73
95
  end
74
96
 
@@ -86,8 +108,9 @@ module MightyGrid
86
108
  def filter_param_name; 'f' end
87
109
 
88
110
  # Get filter name by field
89
- def get_filter_name(filter_name)
90
- "#{name}[#{filter_param_name}][#{filter_name}]"
111
+ def get_filter_name(field, model = nil)
112
+ field_name = model.present? ? "#{model.table_name}.#{field}" : field
113
+ "#{name}[#{filter_param_name}][#{field_name}]"
91
114
  end
92
115
 
93
116
  # Get current grid parameters
@@ -96,9 +119,10 @@ module MightyGrid
96
119
  end
97
120
 
98
121
  # Get order parameters
99
- def order_params(attribute)
100
- direction = attribute.to_s == @mg_params[:order] ? another_order_direction : 'asc'
101
- {@name => {order: attribute, order_direction: direction}}
122
+ def order_params(attribute, model = nil)
123
+ order = model.present? ? "#{model.table_name}.#{attribute}" : attribute.to_s
124
+ direction = order == current_grid_params['order'] ? another_order_direction : 'asc'
125
+ {@name => {order: order, order_direction: direction}}
102
126
  end
103
127
 
104
128
  # Get current order direction
@@ -1,7 +1,7 @@
1
1
  module MightyGrid
2
2
  class Column
3
3
 
4
- attr_reader :attribute, :attrs, :th_attrs, :options, :title
4
+ attr_reader :attribute, :attrs, :th_attrs, :options, :title, :model
5
5
  attr_accessor :render_value
6
6
 
7
7
  def initialize(attr_or_options=nil, options=nil, &block)
@@ -15,6 +15,10 @@ module MightyGrid
15
15
  @attribute = attr_or_options
16
16
  @render_value = @attribute
17
17
  end
18
+
19
+ @model = @options[:model]
20
+ raise MightyGridArgumentError.new("Model of field for filtering should have type ActiveRecord") if @model && @model.superclass != ActiveRecord::Base
21
+
18
22
  @attrs = @options[:html] if @options.has_key?(:html)
19
23
  @th_attrs = @options[:th_html] if @options.has_key?(:th_html)
20
24
  @title = @options.has_key?(:title) && @options[:title] || ''
@@ -23,7 +27,8 @@ module MightyGrid
23
27
  def render(record)
24
28
  case @render_value
25
29
  when String, Symbol
26
- return record[@render_value]
30
+ rec = @model ? record.send(@model.to_s.underscore) : record
31
+ return rec[@render_value]
27
32
  when Proc
28
33
  value = @render_value.call(record)
29
34
  return ERB::Util.h(value).to_s.html_safe
@@ -6,46 +6,53 @@ module MightyGrid
6
6
  @grid = grid
7
7
  end
8
8
 
9
+ # Get <tt>label</tt> HTML tag
9
10
  def label(name, content_or_options = nil, options = nil, &block)
10
- filter_name = @grid.get_filter_name(name).parameterize('_')
11
+ options = content_or_options if content_or_options.is_a?(Hash)
12
+ human_name = (content_or_options.is_a?(Hash) || content_or_options.nil?) ? @grid.klass.human_attribute_name(name) : content_or_options
11
13
 
12
- if content_or_options.is_a?(Hash)
13
- options = content_or_options
14
- else
15
- name ||= content_or_options
16
- end
17
-
18
- name = @grid.klass.human_attribute_name(name)
14
+ f_options = filter_options(name, options, false)
19
15
 
20
- label_tag(filter_name, name, options, &block)
16
+ label_tag(get_filter_id(f_options), human_name, options, &block)
21
17
  end
22
18
 
19
+ # Get <tt>input</tt> HTML tag
23
20
  def text_field(name, options={})
24
- text_field_tag(@grid.get_filter_name(name), get_filter_param(name), options)
21
+ f_options = filter_options(name, options)
22
+ text_field_tag(@grid.get_filter_name(name, f_options[:model]), get_filter_param(name, f_options[:model]), options)
25
23
  end
26
24
 
25
+ # Get <tt>select</tt> HTML tag
27
26
  def select(name, option_tags=nil, options={})
28
27
  @grid.filters[name] = option_tags
29
28
  selected = nil
30
29
  selected = options.delete(:selected) if options.has_key?(:selected)
31
- selected = get_filter_param(name) if get_filter_param(name)
30
+
31
+ f_options = filter_options(name, options)
32
+
33
+ selected = get_filter_param(name, f_options[:model]) if get_filter_param(name, f_options[:model])
32
34
  opts = options_for_select(option_tags, selected)
33
35
 
34
- select_tag(@grid.get_filter_name(name), opts, options)
36
+ select_tag(@grid.get_filter_name(name, f_options[:model]), opts, options)
35
37
  end
36
38
 
39
+ # Get <tt>checkbox</tt>
37
40
  def check_box(name, value = '1', checked = false, options = {})
38
41
  checked = true if get_filter_param(name)
42
+
43
+ f_options = filter_options(name, options)
39
44
 
40
- check_box_tag(@grid.get_filter_name(name), value, checked, options)
45
+ check_box_tag(@grid.get_filter_name(name, f_options[:model]), value, checked, options)
41
46
  end
42
47
 
48
+ # Get button for Apply filter changes
43
49
  def submit(content = nil, options = {})
44
50
  content = I18n.t("mighty_grid.filters.submit", default: 'Apply changes') if content.blank?
45
51
  options.merge!(type: :submit)
46
52
  content_tag(:button, content, options)
47
53
  end
48
54
 
55
+ # Get button for Reset filter changes
49
56
  def reset(content = nil, options = {})
50
57
  content = I18n.t("mighty_grid.filters.reset", default: 'Reset changes') if content.blank?
51
58
  options.merge!(type: :reset)
@@ -54,8 +61,24 @@ module MightyGrid
54
61
 
55
62
  private
56
63
 
57
- def get_filter_param(name)
58
- @grid.filter_params.has_key?(name) ? @grid.get_current_grid_param(@grid.filter_param_name)[name] : nil
64
+ def get_filter_param(name, model = nil)
65
+ filter_name = model ? "#{model.table_name}.#{name}" : name
66
+ @grid.filter_params.has_key?(filter_name) ? @grid.filter_params[filter_name] : nil
67
+ end
68
+
69
+ def get_filter_id(name: nil, model: nil)
70
+ @grid.get_filter_name(name, model).parameterize('_')
71
+ end
72
+
73
+ def filter_options(name, options, with_id = true)
74
+ opts = {name: name}
75
+ if options.is_a?(Hash) && options.has_key?(:model)
76
+ model = options.delete(:model)
77
+ raise MightyGridArgumentError.new("Model of field for filtering should have type ActiveRecord") if model.present? && model.superclass != ActiveRecord::Base
78
+ opts.merge!(model: model)
79
+ options.merge!(id: get_filter_id(opts)) if with_id
80
+ end
81
+ opts
59
82
  end
60
83
  end
61
84
  end
@@ -58,7 +58,7 @@ module MightyGrid
58
58
  rendering.columns.map { |column|
59
59
  content_tag :th, column.th_attrs do
60
60
  if column.options[:ordering] && column.attribute.present?
61
- link_to(column.title, "?#{MightyGrid::MgHash.rec_merge(grid.params, grid.order_params(column.attribute)).except('controller', 'action').to_query}").html_safe
61
+ link_to(column.title, "?#{MightyGrid::MgHash.rec_merge(grid.params, grid.order_params(column.attribute, column.model)).except('controller', 'action').to_query}").html_safe
62
62
  else
63
63
  column.title.html_safe
64
64
  end
@@ -1,3 +1,3 @@
1
1
  module MightyGrid
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -15,6 +15,7 @@
15
15
  <%= grid @users_grid do |g| %>
16
16
  <% g.column :name %>
17
17
  <% g.column :role %>
18
+ <% g.column :is_banned %>
18
19
  <% end %>
19
20
  </body>
20
21
  </html>
@@ -5,7 +5,15 @@ describe MightyGrid::Base do
5
5
  before(:all) do
6
6
  @controller = ActionView::TestCase::TestController.new
7
7
 
8
- @default_options = {page: 1, per_page: 15, name: 'grid', :include => nil, joins: nil}
8
+ @default_options = {
9
+ page: 1,
10
+ per_page: 15,
11
+ name: 'grid',
12
+ :include => nil,
13
+ joins: nil,
14
+ conditions: nil,
15
+ group: nil
16
+ }
9
17
  end
10
18
 
11
19
  describe '#new' do
@@ -102,10 +110,10 @@ describe MightyGrid::Base do
102
110
  before(:all){ @controller.params = {'grid'=>{'order' => 'name', 'order_direction' => 'asc'}} }
103
111
  subject { MightyGrid::Base.new(User, @controller) }
104
112
  context 'with current order attribute' do
105
- it { subject.order_params(:name).should == {'grid'=>{order: :name, order_direction: 'desc'}} }
113
+ it { subject.order_params(:name).should == {'grid'=>{order: 'name', order_direction: 'desc'}} }
106
114
  end
107
115
  context 'with other order attribute' do
108
- it { subject.order_params(:description).should == {'grid'=>{order: :description, order_direction: 'asc'}} }
116
+ it { subject.order_params(:description).should == {'grid'=>{order: 'description', order_direction: 'asc'}} }
109
117
  end
110
118
  after(:all){ @controller.params = {} }
111
119
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mighty_grid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jurrick
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-25 00:00:00.000000000 Z
11
+ date: 2014-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler