index_for 0.1.1 → 0.1.2

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: 1e51681ee6904866e0b057b58feaac06fd709b82
4
- data.tar.gz: 6c98c2b5b7b41ed2c7440464c47e66d4da789553
3
+ metadata.gz: f637db5d2b2459b10bfe9685fc3ccac559bafb13
4
+ data.tar.gz: e4d74aaad50eff2af5a5146e7175f88e1b561500
5
5
  SHA512:
6
- metadata.gz: 2fe646bf96b40a2d47ecaf7678a8a4046aa9ceb46604f7fe333e9e8c390664d4f6dc4682826b796f93730a425ab4b18b0276c4a56dd6910d1d80493a357eef02
7
- data.tar.gz: 89a67513b4904a901470751550efe0fad410f4523585e514547eee15ed3796c428f776a4b1e73b18dc90716143a67de07b8c721e1c1cf57195e82d5913b92575
6
+ metadata.gz: a443df37260f74e1bde7ba93144eef584c5215ce799c615e54cd625c73ef491ad943f5d1645860121013f745e7e6a2288aaea77fc721fd969b9f16e38ab8a8d2
7
+ data.tar.gz: a9ee356a9fa0bced86a328b41088604fd2d5af8d22815ccd83acdfa6983792f02dcc3bdbc2bf39776fb48bf390317090f0fe9ef35b8a440c90d61c902c42c70f
@@ -3,7 +3,7 @@ module IndexFor
3
3
 
4
4
  private
5
5
 
6
- def attribute_value attribute_name, options, &block
6
+ def attribute_value attribute_name, options
7
7
  attribute_name = options[:value] if options[:value]
8
8
  attribute_name = :"#{attribute_name}.#{options[:with]}" if options[:with]
9
9
 
@@ -15,9 +15,7 @@ module IndexFor
15
15
  object = object.send(attribute_name)
16
16
  end if parts.any?
17
17
 
18
- if block
19
- nil
20
- elsif object.respond_to?(:"human_#{attribute_name}")
18
+ if object.respond_to?(:"human_#{attribute_name}")
21
19
  object.send :"human_#{attribute_name}"
22
20
  else
23
21
  object.send(attribute_name)
@@ -61,23 +61,25 @@ module IndexFor
61
61
  class_name.join("_").to_sym
62
62
  end
63
63
 
64
- def wrap_content_with type, content, options = {}, &block #:nodoc:
64
+ def wrap_attribute_with type, attribute, options = {}, &block
65
65
  type_tag, type_html_options = apply_html type, options
66
- append_class type_html_options, IndexFor.blank_content_class if content.blank?
66
+
67
+ if block
68
+ content = block
69
+ else
70
+ content = attribute_value attribute, options
71
+ append_class type_html_options, IndexFor.blank_content_class if content.blank?
72
+ end
67
73
 
68
74
  @template.content_tag type_tag, type_html_options do
69
- format_content(content, options, &block)
75
+ format_content(content, options)
70
76
  end
71
77
  end
72
78
 
73
- def format_content content, options = {}, &block
79
+ def format_content content, options = {}
74
80
  # We need to convert content to_a because when dealing with ActiveRecord
75
81
  # Array proxies, the follow statement Array# === content return false
76
- if block && block.arity <= 1
77
- content = block
78
- elsif content.respond_to?(:to_ary)
79
- content = content.to_a
80
- end
82
+ content = content.to_ary if content.respond_to?(:to_ary)
81
83
 
82
84
  case content
83
85
  when Date, Time, DateTime
@@ -86,15 +88,23 @@ module IndexFor
86
88
  translate :"index_for.yes", :default => "Yes"
87
89
  when FalseClass
88
90
  translate :"index_for.no", :default => "No"
89
- when Array, Hash
90
- content.empty? ? blank_content(options) :
91
- collection_content(content, options, &block)
92
- when Proc
93
- @template.capture(@object, &content)
94
91
  when NilClass
95
92
  blank_content(options)
93
+ when Proc
94
+ @template.capture(@object, &content)
96
95
  else
97
- content.to_s
96
+ formatter = options[:format] && IndexFor.formatters[options[:format]]
97
+
98
+ if formatter
99
+ @template.instance_exec(content, @object, &formatter)
100
+ else
101
+ case content
102
+ when Array, Hash
103
+ content.empty? ? blank_content(options) : collection_content(content, options)
104
+ else
105
+ content.to_s
106
+ end
107
+ end
98
108
  end
99
109
  end
100
110
 
@@ -102,16 +112,12 @@ module IndexFor
102
112
  options[:if_blank] || translate(:blank, :default => "Not specified")
103
113
  end
104
114
 
105
- def collection_content collection, options, &block
115
+ def collection_content collection, options
106
116
  collection_tag = options[:collection_tag] || IndexFor.collection_tag
107
117
  collection_column_tag = options[:collection_column_tag] || IndexFor.collection_column_tag
108
118
  @template.content_tag collection_tag do
109
119
  collection.map do |content|
110
- if block
111
- @template.capture content, collection, @object, &block
112
- else
113
- @template.content_tag collection_column_tag, content
114
- end
120
+ @template.content_tag collection_column_tag, content
115
121
  end.join.html_safe
116
122
  end
117
123
  end
@@ -5,8 +5,7 @@ module IndexFor
5
5
 
6
6
  def attribute attribute_name, options = {}, &block
7
7
  append_html_class options, attribute_class_name(attribute_name)
8
- wrap_content_with :table_body_cell, attribute_value(attribute_name,
9
- options, &block), options, &block
8
+ wrap_attribute_with :table_body_cell, attribute_name, options, &block
10
9
  end
11
10
 
12
11
  def actions *action_names, &block
@@ -24,8 +24,7 @@ module IndexFor
24
24
 
25
25
  def list_content attribute_name, options, &block
26
26
  append_html_class options, attribute_class_name(attribute_name)
27
- wrap_content_with :list_content, attribute_value(attribute_name,
28
- options), options, &block
27
+ wrap_attribute_with :list_content, attribute_name, options, &block
29
28
  end
30
29
 
31
30
  end
@@ -1,3 +1,3 @@
1
1
  module IndexFor
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/index_for.rb CHANGED
@@ -14,7 +14,7 @@ module IndexFor
14
14
  @@table_tag = :table
15
15
  mattr_accessor :table_class
16
16
  @@table_class = nil
17
-
17
+
18
18
  mattr_accessor :table_head_tag
19
19
  @@table_head_tag = :thead
20
20
  mattr_accessor :table_head_class
@@ -34,7 +34,7 @@ module IndexFor
34
34
  @@table_head_cell_tag = :th
35
35
  mattr_accessor :table_head_cell_class
36
36
  @@table_head_cell_class = nil
37
-
37
+
38
38
  mattr_accessor :table_body_cell_tag
39
39
  @@table_body_cell_tag = :td
40
40
  mattr_accessor :table_body_cell_class
@@ -93,4 +93,10 @@ module IndexFor
93
93
  def self.setup
94
94
  yield self
95
95
  end
96
+
97
+ mattr_accessor :formatters
98
+ @@formatters = {}
99
+ def self.format key, &block
100
+ formatters[key] = block
101
+ end
96
102
  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.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-22 00:00:00.000000000 Z
11
+ date: 2015-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel