index_for 0.3.0 → 0.3.1

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: fd2792a5f94dea8fb9f4a3a2f9da83dffa4c684b
4
- data.tar.gz: 9e8adff35f4d147c0403dbb13ef931aae14677e1
3
+ metadata.gz: 925b8036067c5c22249a884e6159c2c9921c1c2d
4
+ data.tar.gz: 3e65dec6318633705463a0b9a5de2029d29d8195
5
5
  SHA512:
6
- metadata.gz: 6e033886586e5dbbf81c6f85c29810b44558e89b8bffda374e7bf9d514f31f77506b52ea62738e1e66cc00de3963862a1c8d8a1ae40a5971ca5f3c6713be194a
7
- data.tar.gz: 31580f4b85224ccfb321a9fd98d5d7a08d98f7ab5029b17b7c50bbd4a9b54b8090e2bada7b3344b85859f1a739943713840049e27114fdda29ba89b1ff59ef3c
6
+ metadata.gz: 82fd862e2f45217f5fffc8dc086d9966c026f1229c49ac00b8f0a8cd62669c4e3461c36c5393388a53b855f494d113232b1be37b173320085b64d165089b4af2
7
+ data.tar.gz: 978fc9786afe8f83da7567686fd2896756a6101d2b494902328f6a84a8d2e14d1797a86566ad51663cc95df112effe4d4335f23a2832439572cebe291851e226
@@ -25,11 +25,20 @@ module IndexFor
25
25
  end
26
26
 
27
27
  def fields_for attribute_name, options = {}, &block
28
- object = @object.send(attribute_name)
29
- fields_for = @html_options[:fields_for] || []
28
+ object = @object
29
+ html_options = @html_options
30
+
31
+ @object = @object.send(attribute_name)
32
+ fields_for = @html_options[:fields_for] ? @html_options[:fields_for].clone : []
30
33
  fields_for.push attribute_name
31
- options.merge!(fields_for: attribute_name)
32
- @template.capture(self.class.new(object, options, @template), &block)
34
+ @html_options = html_options.merge(options).merge(fields_for: fields_for)
35
+
36
+ result = @template.capture(self, &block)
37
+
38
+ @object = object
39
+ @html_options = html_options
40
+
41
+ result
33
42
  end
34
43
 
35
44
  def actions *action_names, █ end
@@ -44,7 +53,7 @@ module IndexFor
44
53
  end
45
54
 
46
55
  def apply_html_options type, options = {}
47
- type_class = IndexFor.send :"#{type}_class"
56
+ type_class = IndexFor.try(:"#{type}_class")
48
57
 
49
58
  type_html_options = {}
50
59
  type_html_options.merge!(html_options[:"#{type}_html"]) if html_options[:"#{type}_html"]
@@ -8,8 +8,7 @@ module IndexFor
8
8
  @template.capture(@object, &block)
9
9
  else
10
10
  object = @html_options[:namespace] ? @html_options[:namespace].clone.push(@object) : @object
11
- action_title = translate(:"actions.#{action_name}",
12
- default: action_name.to_s.humanize).html_safe
11
+ action_title = (translate(:"actions.#{action_name}") || action_name.to_s.humanize).html_safe
13
12
  action_html_options = apply_html_options :action_link, options
14
13
  append_class action_html_options, :"action_#{action_name}", options[:class]
15
14
  action_html_options[:data] ||= {}
@@ -5,17 +5,34 @@ module IndexFor
5
5
  attr_accessor :per_page, :page, :order, :direction, :search
6
6
 
7
7
  def sortable_fields
8
- @sortable_fields ||= []
8
+ @sortable_fields ||= {}
9
9
  end
10
10
 
11
11
  def searchable_fields
12
12
  @searchable_fields ||= []
13
13
  end
14
14
 
15
+ def filterable_fields
16
+ @filterable_fields ||= {}
17
+ end
18
+
19
+ def parse_option option, default
20
+ option === true ? default : option if option
21
+ end
22
+
23
+ def column_exists? field_name
24
+ ActiveRecord::Base.connection.column_exists? *field_name.split(".", 2)
25
+ end
26
+
15
27
  def attribute attribute_name, options = {}
16
28
  model_class = options[:model] || html_options[:model] || @object
17
- sortable_fields << (options[:sortable] || "#{model_class.table_name}.#{attribute_name}")
18
- searchable_fields << "#{model_class.table_name}.#{attribute_name}" if options[:searchable]
29
+ field_name = "#{model_class.table_name}.#{attribute_name}"
30
+ sortable_field = parse_option(options[:sortable] || true, field_name)
31
+ sortable_fields.merge!(field_name => sortable_field) if sortable_field
32
+ searchable_field = parse_option(options[:searchable] || false, field_name)
33
+ searchable_fields << searchable_field if searchable_field
34
+ filterable_field = parse_option(options[:filterable] || false, field_name)
35
+ filterable_fields.merge! filterable_field if filterable_field
19
36
  nil
20
37
  end
21
38
 
@@ -32,14 +49,15 @@ module IndexFor
32
49
 
33
50
  self.per_page = (params[:per_page] || IndexFor.per_page).to_i
34
51
  self.page = (params[:page] || 1).to_i
35
- self.order = params[:order] if sortable_fields.include? params[:order]
52
+ self.order = sortable_fields[params[:order]] if sortable_fields.key? params[:order]
36
53
  self.direction = params[:direction] || "asc"
37
54
  self.search = params[:search]
38
55
 
39
56
  collection = @object
40
57
  collection = collection.page(page).per(per_page)
41
- collection = collection.order("#{order} #{direction.upcase}") if order
42
- collection = collection.where(searchable_fields.map{|f|"#{f} LIKE '%#{search}%'"}.join(" OR ")) if search
58
+ collection = collection.order(Array.wrap(order).select{|f|column_exists? f}.map{|f|"#{f} #{direction.upcase}"}.join(", ")) if order
59
+ collection = collection.where((searchable_fields.flatten.map{|f|"#{f} LIKE '%#{search}%'"} + filterable_fields.select{|k,v|v}.map{|k,v|"#{k} = #{v}"}).join(" OR ")) if search
60
+
43
61
  collection
44
62
  end
45
63
 
@@ -6,7 +6,7 @@ module IndexFor
6
6
 
7
7
  def attribute attribute_name, options = {}
8
8
  model_class = options[:model] || html_options[:model] || @object.class
9
- order = options[:sortable] || "#{model_class.table_name}.#{attribute_name}"
9
+ order = "#{model_class.table_name}.#{attribute_name}"
10
10
 
11
11
  params = @template.params
12
12
  direction = params[:direction] || "asc"
@@ -128,9 +128,10 @@ module IndexFor
128
128
  append_class row_html_options, IndexFor.table_row_class
129
129
 
130
130
  content_tag(body_tag, body_html_options) do
131
+ builder = html_options[:body_builder] || IndexFor::BodyColumnBuilder
132
+ builder = builder.new(objects, html_options, self)
131
133
  objects.map do |object|
132
- builder = html_options[:body_builder] || IndexFor::BodyColumnBuilder
133
- builder = builder.new(object, html_options, self)
134
+ builder.object = object
134
135
  content = capture(builder, &block)
135
136
  content_tag(row_tag, content, row_html_options)
136
137
  end.join.html_safe
@@ -1,3 +1,3 @@
1
1
  module IndexFor
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: index_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-22 00:00:00.000000000 Z
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel