index_for 0.1.3 → 0.2.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: ac386c403aa8a63b5f340d91af1efc33b6115bef
4
- data.tar.gz: 1d72b0b7947d7d49baf0fe80c731125b7ffbbd82
3
+ metadata.gz: 26b5255be6caaa47b9cc3cef7075cced013a4492
4
+ data.tar.gz: c1fc66fe0061e20a197a41ad68f94ecefba500a1
5
5
  SHA512:
6
- metadata.gz: 2ab8b1cebd1d5081d921f2af165a41a55691fce9fb14c4a8d89e36204af253e639a302e6922f98206586f611d0ab94601cd0ffe1eba09339665b148f07a7c9de
7
- data.tar.gz: b32ebeebc86daa0d277d5272c20d84ea2ac376aadccbf12d58ff1245cc29cf9915c61c75de8bcc3c6f3cf9b045cdc603273b1e4cb7d6eaa29884864407e834cf
6
+ metadata.gz: dd9f117a518441292483f6adc82a68e46382f7d29566aa8dc5c7cd37f28fa2c62591df261134616b61b69799828a0da69b4c9b9f452bf310e784f5c9c7036f0b
7
+ data.tar.gz: 7bb0e089ac539e666e47873cffb82283cf346ba69e82bcfe42e3f27474f5a1095092f2f025ff10b77288edd80e9a1a0391ac4a731f0b00f5a67196469225d777
@@ -5,6 +5,11 @@ module IndexFor
5
5
  class_option :template_engine, :desc => 'Template engine to be invoked (erb or haml or slim).'
6
6
  source_root File.expand_path('../templates', __FILE__)
7
7
 
8
+ def copy_wice_index_for_template
9
+ engine = options[:template_engine]
10
+ copy_file "_wice_index_for.html.#{engine}", "app/views/index_for/_wice_index_for.html.#{engine}"
11
+ end
12
+
8
13
  def copy_initializers
9
14
  copy_file 'index_for.rb', 'config/initializers/index_for.rb'
10
15
  end
@@ -0,0 +1,65 @@
1
+ <div class="form-inline wice_index_for">
2
+ <div class="row">
3
+ <div class="col-sm-6 index_for_length">
4
+ Show
5
+ <select class="form-control input">
6
+ <% builder.per_pages do |per_page, url, active| %>
7
+ <option data-href="<%= url %>" <%= "selected" if active %>><%= per_page %></option>
8
+ <% end %>
9
+ </select>
10
+ entries
11
+ </div>
12
+
13
+ <div class="col-sm-6 text-right index_for_filter">
14
+ Search:
15
+ <input type="search" class="form-control input" placeholder="<%= params[:search] %>" data-href="<%= url_for(params.merge(search: nil)) %>">
16
+ </div>
17
+ </div>
18
+
19
+ <div class="row">
20
+ <div class="col-sm-12">
21
+ <% html_options[:head_builder] ||= IndexFor::WiceHeadColumnBuilder %>
22
+ <%= index_for objects, html_options, &block %>
23
+ </div>
24
+ </div>
25
+
26
+ <div class="row">
27
+ <div class="col-sm-5 index_for_info"><%= page_entries_info objects %></div>
28
+ <div class="col-sm-7 text-right index_for_pagination">
29
+ <%= paginate objects, window: 2 %>
30
+ </div>
31
+ </div>
32
+ </div>
33
+
34
+ <script type="text/javascript">
35
+ $(document).on("ready page:load page:restore", function() {
36
+ $(".wice_index_for").each(function() {
37
+ var $this = $(this);
38
+ if ($this.data("wice-index-for")) return;
39
+ $this.data("wice-index-for", true);
40
+ $this.find(".index_for_length select").change(function(event) {
41
+ var location = $(event.target).find("option:selected").data("href");
42
+ if (location) window.location = location;
43
+ });
44
+
45
+ $this.find(".index_for thead th").click(function(event) {
46
+ var location = $(event.target).data("href");
47
+ if (location) window.location = location;
48
+ });
49
+
50
+ $this.find(".index_for_filter input").keypress(function(event) {
51
+ if (event.which === 13 || event.keyCode === 13) {
52
+ var location = $(event.target).data("href"),
53
+ search = $(event.target).val();
54
+ if (location) {
55
+ if (location.indexOf("?") >= 0) {
56
+ window.location = location + "&search=" + search;
57
+ } else {
58
+ window.location = location + "?search=" + search;
59
+ }
60
+ }
61
+ }
62
+ })
63
+ })
64
+ });
65
+ </script>
@@ -0,0 +1,20 @@
1
+ .form-inline.wice_index_for
2
+ .row
3
+ .col-sm-6.index_for_length
4
+ Show
5
+ %select.form-control.input
6
+ - builder.per_pages do |per_page, url, active|
7
+ %option{ "data-href" => url, "selected" => active }
8
+ = per_page
9
+ entries
10
+ .col-sm-6.text-right.index_for_filter
11
+ Search:
12
+ %input.form-control.input{ "type" => "search", "placeholder" => params[:search], "data-href" => url_for(params.merge(search: nil)) }
13
+ .row
14
+ .col-sm-12
15
+ - html_options[:head_builder] ||= IndexFor::WiceHeadColumnBuilder
16
+ = index_for objects, html_options, block
17
+ .row
18
+ .col-sm-5.index_for_info= page_entries_info objects
19
+ .col-sm-7.text-right.index_for_pagination
20
+ = paginate objects, window: 2
@@ -0,0 +1,20 @@
1
+ .form-inline.wice_index_for
2
+ .row
3
+ .col-sm-6.index_for_length
4
+ | Show
5
+ select.form-control.input
6
+ - builder.per_pages do |per_page, url, active|
7
+ option data-href=url selected=(active )
8
+ = per_page
9
+ | entries
10
+ .col-sm-6.text-right.index_for_filter
11
+ | Search:
12
+ input.form-control.input type="search" placeholder=params[:search] data-href=(url_for(params.merge(search: nil)) )
13
+ .row
14
+ .col-sm-12
15
+ - html_options[:head_builder] ||= IndexFor::WiceHeadColumnBuilder
16
+ = index_for objects, html_options, block
17
+ .row
18
+ .col-sm-5.index_for_info= page_entries_info objects
19
+ .col-sm-7.text-right.index_for_pagination
20
+ = paginate objects, window: 2
@@ -8,18 +8,12 @@ module IndexFor
8
8
  attribute_name = :"#{attribute_name}.#{options[:with]}" if options[:with]
9
9
 
10
10
  parts = attribute_name.to_s.split(".")
11
- attribute_name = parts.pop
12
11
 
13
12
  object = @object
14
13
  parts.each do |attribute_name|
15
14
  object = object.send(attribute_name)
16
- end if parts.any?
17
-
18
- if object.respond_to?(:"human_#{attribute_name}")
19
- object.send :"human_#{attribute_name}"
20
- else
21
- object.send(attribute_name)
22
15
  end
16
+ object
23
17
  rescue
24
18
  options.key?(:if_raise) ? options[:if_raise] : raise
25
19
  end
@@ -81,32 +81,26 @@ module IndexFor
81
81
  # Array proxies, the follow statement Array# === content return false
82
82
  content = content.to_ary if content.respond_to?(:to_ary)
83
83
 
84
+ formatter = options[:format] && IndexFor.formatters[options[:format]]
85
+ return @template.instance_exec(content, @object, &formatter) if formatter
86
+
84
87
  case content
85
- when Date, Time, DateTime
86
- I18n.l content, :format => options[:format] || IndexFor.i18n_format
88
+ when String
89
+ content.empty? ? blank_content(options) : content
87
90
  when TrueClass
88
91
  translate :"index_for.yes", :default => "Yes"
89
92
  when FalseClass
90
93
  translate :"index_for.no", :default => "No"
91
94
  when NilClass
92
95
  blank_content(options)
96
+ when Date, Time, DateTime
97
+ I18n.l content, :format => options[:format] || IndexFor.i18n_format
98
+ when Array, Hash
99
+ content.empty? ? blank_content(options) : collection_content(content, options)
93
100
  when Proc
94
101
  @template.capture(@object, &content)
95
102
  else
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
- when String
105
- content.empty? ? blank_content(options) : content
106
- else
107
- content.to_s
108
- end
109
- end
103
+ content.to_s
110
104
  end
111
105
  end
112
106
 
@@ -0,0 +1,53 @@
1
+ require 'index_for/builder'
2
+
3
+ module IndexFor
4
+ class WiceBuilder < Builder
5
+ attr_accessor :per_page, :page, :order, :direction, :search
6
+
7
+ def sortable_fields
8
+ @sortable_fields ||= []
9
+ end
10
+
11
+ def searchable_fields
12
+ @searchable_fields ||= []
13
+ end
14
+
15
+ def attribute attribute_name, options = {}
16
+ 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]
19
+ nil
20
+ end
21
+
22
+ def fields_for attribute_name, options = {}, &block
23
+ cached_html_options = @html_options
24
+ @html_options = options
25
+ block.call self
26
+ @html_options = cached_html_options
27
+ nil
28
+ end
29
+
30
+ def result
31
+ params = @template.params
32
+
33
+ self.per_page = (params[:per_page] || IndexFor.per_page).to_i
34
+ self.page = (params[:page] || 1).to_i
35
+ self.order = params[:order] if sortable_fields.include? params[:order]
36
+ self.direction = params[:direction] || "asc"
37
+ self.search = params[:search]
38
+
39
+ collection = @object
40
+ 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
43
+ collection
44
+ end
45
+
46
+ def per_pages
47
+ IndexFor.per_pages.each do |per_page|
48
+ yield per_page, @template.url_for(template.params.merge(page: nil, per_page: per_page)), per_page == self.per_page
49
+ end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,35 @@
1
+ require 'index_for/builder'
2
+ require 'index_for/builders/head_column_builder'
3
+
4
+ module IndexFor
5
+ class WiceHeadColumnBuilder < HeadColumnBuilder
6
+
7
+ def attribute attribute_name, options = {}
8
+ model_class = options[:model] || html_options[:model] || @object.class
9
+ order = options[:sortable] || "#{model_class.table_name}.#{attribute_name}"
10
+
11
+ params = @template.params
12
+ direction = params[:direction] || "asc"
13
+ reverse_direction = if order != params[:order] || params[:direction] && params[:direction] == "desc"
14
+ "asc"
15
+ else
16
+ "desc"
17
+ end
18
+
19
+ sorting_class = if order == params[:order]
20
+ "sorting #{direction}"
21
+ else
22
+ "sorting" if params[:sortable] != false
23
+ end
24
+
25
+ append_html_class options, attribute_class_name(attribute_name), sorting_class
26
+
27
+ options[:html] ||= {}
28
+ options[:html][:data] ||= {}
29
+ options[:html][:data][:href] = @template.url_for(@template.params.merge(order: order, direction: reverse_direction))
30
+
31
+ wrap_with :table_head_cell, attribute_label(attribute_name, options), options
32
+ end
33
+
34
+ end
35
+ end
@@ -14,16 +14,9 @@ module IndexFor
14
14
  def index_for objects, html_options = {}, &block
15
15
  html_options = html_options.dup
16
16
 
17
- tag = html_options[:table_tag] || IndexFor.table_tag
18
-
19
- if objects.is_a?(Array) &&
20
- objects.find do |o|
21
- !o.is_a?(Symbol) && !o.is_a?(String) && !o.is_a?(ActiveRecord::Base)
22
- end == objects.last
23
- html_options[:namespace] = objects
24
- objects = objects.pop
25
- end
17
+ objects = fetch_objects objects, html_options
26
18
 
19
+ tag = html_options[:table_tag] || IndexFor.table_tag
27
20
  klass = html_options[:klass] || objects.try(:klass) || objects.first.class
28
21
 
29
22
  html_options[:id] ||= index_for_id(klass)
@@ -36,6 +29,23 @@ module IndexFor
36
29
  content_tag(tag, content, html_options)
37
30
  end
38
31
 
32
+ def wice_index_for objects, html_options = {}, &block
33
+ html_options = html_options.dup
34
+
35
+ objects = fetch_objects objects, html_options
36
+
37
+ builder = html_options[:builder] || IndexFor::WiceBuilder
38
+ builder = builder.new(objects, html_options, self)
39
+ block.call builder
40
+
41
+ render partial: "index_for/wice_index_for", locals: {
42
+ builder: builder,
43
+ objects: builder.result,
44
+ html_options: html_options,
45
+ block: block
46
+ }
47
+ end
48
+
39
49
  # Create action links and yields a builder.
40
50
  #
41
51
  # Example:
@@ -51,7 +61,8 @@ module IndexFor
51
61
  html_options = action_names.extract_options!
52
62
  action_names = [:show, :edit, :destroy] if action_names == [:all]
53
63
 
54
- builder = IndexFor::ActionBuilder.new(object, html_options, self)
64
+ builder = html_options[:action_builder] || IndexFor::ActionBuilder
65
+ builder = builder.new(object, html_options, self)
55
66
 
56
67
  content = capture(builder) do |a|
57
68
  action_names.map do |action_name|
@@ -81,7 +92,8 @@ module IndexFor
81
92
  html_options[:id] ||= show_for_id(object)
82
93
  html_options[:class] = show_for_class(object, html_options)
83
94
 
84
- content = capture(IndexFor::ListColumnBuilder.new(object, html_options, self), &block)
95
+ builder = html_options[:builder] || IndexFor::ListColumnBuilder
96
+ content = capture(builder.new(object, html_options, self), &block)
85
97
 
86
98
  content_tag(tag, content, html_options)
87
99
  end
@@ -98,7 +110,8 @@ module IndexFor
98
110
  row_html_options = html_options[:row_html] || {}
99
111
  append_class row_html_options, IndexFor.table_row_class, dom_class(object)
100
112
 
101
- content = capture(IndexFor::HeadColumnBuilder.new(object, html_options, self), &block)
113
+ builder = html_options[:head_builder] || IndexFor::HeadColumnBuilder
114
+ content = capture(builder.new(object, html_options, self), &block)
102
115
 
103
116
  content_tag(head_tag, head_html_options) do
104
117
  content_tag(row_tag, content, row_html_options)
@@ -107,23 +120,36 @@ module IndexFor
107
120
 
108
121
  def index_for_body objects, html_options = {}, &block
109
122
  body_tag = html_options[:body_tag] || IndexFor.table_body_tag
110
- row_tag = html_options[:row_tag] || IndexFor.table_row_tag
111
-
112
123
  body_html_options = html_options[:body_html] || {}
113
124
  append_class body_html_options, IndexFor.table_body_class
114
125
 
126
+ row_tag = html_options[:row_tag] || IndexFor.table_row_tag
127
+ row_html_options = html_options[:row_html] || {}
128
+ append_class row_html_options, IndexFor.table_row_class
129
+
115
130
  content_tag(body_tag, body_html_options) do
116
131
  objects.map do |object|
117
- row_html_options = html_options[:row_html] || {}
118
- row_html_options[:id] ||= dom_id(object)
119
- append_class row_html_options, IndexFor.table_row_class, dom_class(object)
120
-
121
- content = capture(IndexFor::BodyColumnBuilder.new(object, html_options, self), &block)
132
+ builder = html_options[:body_builder] || IndexFor::BodyColumnBuilder
133
+ builder = builder.new(object, html_options, self)
134
+ content = capture(builder, &block)
122
135
  content_tag(row_tag, content, row_html_options)
123
136
  end.join.html_safe
124
137
  end
125
138
  end
126
139
 
140
+ def fetch_objects objects, html_options
141
+ if objects.is_a?(Array) &&
142
+ objects.find do |o|
143
+ !o.is_a?(Symbol) && !o.is_a?(String) && !o.is_a?(ActiveRecord::Base)
144
+ end == objects.last
145
+ html_options[:namespace] = objects
146
+ objects = objects.pop
147
+ else
148
+ objects
149
+ end
150
+ end
151
+
152
+
127
153
  # Finds the class representing the collection
128
154
  # IndexFor
129
155
  def index_for_id klass
@@ -1,3 +1,3 @@
1
1
  module IndexFor
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/index_for.rb CHANGED
@@ -8,6 +8,8 @@ module IndexFor
8
8
  autoload :HeadColumnBuilder, 'index_for/builders/head_column_builder'
9
9
  autoload :BodyColumnBuilder, 'index_for/builders/body_column_builder'
10
10
  autoload :ListColumnBuilder, 'index_for/builders/list_column_builder'
11
+ autoload :WiceBuilder, 'index_for/builders/wice_builder'
12
+ autoload :WiceHeadColumnBuilder, 'index_for/builders/wice_head_column_builder'
11
13
 
12
14
  # IndexFor
13
15
  mattr_accessor :table_tag
@@ -84,6 +86,12 @@ module IndexFor
84
86
  mattr_accessor :collection_column_tag
85
87
  @@collection_column_tag = :li
86
88
 
89
+ # Wice IndexFor
90
+ mattr_accessor :per_pages
91
+ @@per_pages = [10, 25, 50, 100]
92
+ mattr_accessor :per_page
93
+ @@per_page = @@per_pages.first
94
+
87
95
  # Yield self for configuration block:
88
96
  #
89
97
  # IndexFor.setup do |config|
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.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-27 00:00:00.000000000 Z
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -108,6 +108,9 @@ files:
108
108
  - Rakefile
109
109
  - lib/generators/index_for/USAGE
110
110
  - lib/generators/index_for/install_generator.rb
111
+ - lib/generators/index_for/templates/_wice_index_for.html.erb
112
+ - lib/generators/index_for/templates/_wice_index_for.html.haml
113
+ - lib/generators/index_for/templates/_wice_index_for.html.slim
111
114
  - lib/generators/index_for/templates/en.yml
112
115
  - lib/generators/index_for/templates/index.html.erb
113
116
  - lib/generators/index_for/templates/index.html.haml
@@ -123,6 +126,8 @@ files:
123
126
  - lib/index_for/builders/body_column_builder.rb
124
127
  - lib/index_for/builders/head_column_builder.rb
125
128
  - lib/index_for/builders/list_column_builder.rb
129
+ - lib/index_for/builders/wice_builder.rb
130
+ - lib/index_for/builders/wice_head_column_builder.rb
126
131
  - lib/index_for/helper.rb
127
132
  - lib/index_for/share_helper.rb
128
133
  - lib/index_for/version.rb