greedo 0.0.2 → 0.0.7

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
- SHA1:
3
- metadata.gz: a89bac7cd8d24f99e8f427fdfa36cacd8b1f1d5b
4
- data.tar.gz: c13b4badf51810de63f666cc1375ea9132b9798f
2
+ SHA256:
3
+ metadata.gz: 41e6db7010b9c6b8f6ae92d169ed8ae92ed39ab6dae893b3b8329e9607c66b4f
4
+ data.tar.gz: '082944928cb20ae268e60aa2e8a4b7c43d18fb50035d35093935dba9a558f042'
5
5
  SHA512:
6
- metadata.gz: 2ca476abd4c6d6b02a5f60db77e6eefeb99efa9695eb34ac81449c39eb5ffb31efbf17b8e402438bc9b87b1110bfa9ef7d5ed4bd52ee9408ff09d055f2efc876
7
- data.tar.gz: 8b953f2160f9b37ca5ce3f15bb6f9eea5519fa04f7a8b4cc977876e352e82aecd35736b23b694f4c726e12e5f5121f9cbc9f786a327975cf239fdf484e5125cb
6
+ metadata.gz: 792af4f3c7d45b7d371a49c08dfc411dc0d212366fe6851d85139e9d71a28fd02dd9fe9532c27275d84ef7ad5828a4248243fb62506b575b319c3914f09626c6
7
+ data.tar.gz: df4db7d6e7cd53ab6cee2144284f61e3be1e65f1bf23cf96f886fff5a32220ca867185c2e7919425be4b367850924b687250203648302d05131624b3fdd1145d
data/README.md CHANGED
@@ -30,9 +30,19 @@ After installing greedo, you can use the helper like this:
30
30
  ```
31
31
 
32
32
  This will create a data table with two columns, one labelled "Name" and the other "Actions".
33
- It will show 10 users from the given scope (which should either be an ActiveRecord::Relation or an Array).
33
+ It will show 10 users from the given scope (which should either be an ActiveRecord::Relation or an Array).
34
34
  Pagination will be added if necessary.
35
35
 
36
+ ### Custom empty message
37
+
38
+ ```haml
39
+ %h1 My first table
40
+
41
+ = greedo(User.registered, per_page: 10) do |g|
42
+ = g.custom_empty_message "There are no users in the database."
43
+ = g.column :name
44
+ ```
45
+
36
46
  ### Presenters
37
47
 
38
48
  You can wrap records in a class instance:
@@ -5,13 +5,24 @@
5
5
  %thead
6
6
  %tr
7
7
  - grid.headers.each do |header|
8
- %th= header.label
8
+ %th{class: header.klass}
9
+ .greedo-header
10
+ = header.label
11
+ - if header.sort
12
+ .greedo-order
13
+ = link_to header.order_asc_path do
14
+ %i.glyphicon{class: header.order_asc_class}
15
+ = link_to header.order_desc_path do
16
+ %i.glyphicon{class: header.order_desc_class}
9
17
 
10
18
  %tbody
11
19
  - grid.rows.each do |row|
12
20
  %tr{id: row.id}
13
21
  - row.columns.each do |column|
14
- %td= column.value
22
+ %td{class: column.klass}
23
+ = column.value
15
24
 
16
25
  - if grid.show_pagination?
17
- = will_paginate grid.paginator.records, param_name: param_name, renderer: BootstrapPagination::Rails
26
+ = will_paginate grid.paginator.records, param_name: param_name,
27
+ params: path_params,
28
+ renderer: BootstrapPagination::Rails
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec-core"
24
24
  spec.add_development_dependency "rspec-rails"
25
25
  spec.add_development_dependency "capybara"
26
- spec.add_development_dependency "rails"
26
+ spec.add_development_dependency "rails", "~> 4.2"
27
27
  spec.add_development_dependency "sqlite3"
28
28
 
29
29
  spec.add_dependency "haml"
@@ -1,33 +1,50 @@
1
1
  module Greedo
2
2
  module GridHelper
3
3
  class Grid
4
- attr_reader :paginator, :view_context, :fields, :presenter
4
+ attr_accessor :paginator
5
+ attr_reader :view_context, :fields, :presenter, :empty_message_text,
6
+ :order, :order_by, :path_params
5
7
 
6
- def initialize(paginator:, view_context:)
7
- @paginator = paginator
8
+ def initialize(view_context:, order: nil, order_by: nil, path_params: nil)
8
9
  @view_context = view_context
9
10
  @row_id = ->(record) { default_row_id(record) }
10
11
  @fields = []
11
12
  @presenter = proc{|r| r}
13
+ @empty_message_text = "No data to show."
14
+ @order = order
15
+ @order_by = order_by
16
+ @path_params = path_params
12
17
  end
13
18
 
14
19
  def configure
15
20
  yield self if block_given?
16
21
  end
17
22
 
23
+ def custom_empty_message(empty_message_text)
24
+ @empty_message_text = empty_message_text
25
+ nil
26
+ end
27
+
28
+ def ordered_by
29
+ fields.find(&:ordered_by?) || Field.new
30
+ end
31
+
18
32
  def row_id(&block)
19
33
  @row_id = block
20
34
  nil
21
35
  end
22
36
 
23
- def column(name, label: name.to_s.humanize, &block)
37
+ def column(name, label: name.to_s.humanize, sort: nil, &block)
38
+ if sort.nil? && name.is_a?(Symbol)
39
+ sort = name.to_s
40
+ end
24
41
  if block
25
42
  renderer = ->(record) { view_context.capture(present(record), &block) }
26
43
  else
27
44
  renderer = ->(record) { present(record).public_send(name) }
28
45
  end
29
46
 
30
- fields << Field.new(name, label, renderer)
47
+ fields << Field.new(name, label, renderer, order, order_by, view_context, sort, path_params)
31
48
  nil
32
49
  end
33
50
 
@@ -60,7 +77,7 @@ module Greedo
60
77
  end
61
78
 
62
79
  def empty_message
63
- view_context.content_tag(:p) { "No data to show." }
80
+ view_context.content_tag(:p) { empty_message_text }
64
81
  end
65
82
 
66
83
  private
@@ -74,7 +91,7 @@ module Greedo
74
91
  "#{snake_class}-#{record.id}"
75
92
  end
76
93
 
77
- Column = Struct.new(:value)
94
+ Column = Struct.new(:value, :klass)
78
95
 
79
96
  Row = Struct.new(:record, :row_id, :fields) do
80
97
  def id
@@ -82,26 +99,82 @@ module Greedo
82
99
  end
83
100
 
84
101
  def columns
85
- fields.map{ |f| Column.new(f.value(record)) }
102
+ fields.map{ |f| Column.new(f.value(record), f.klass) }
86
103
  end
87
104
  end
88
105
 
89
- Field = Struct.new(:name, :label, :renderer) do
106
+ Field = Struct.new(:name, :label, :renderer, :order, :order_by,
107
+ :view_context, :sort, :path_params) do
90
108
  def value(record)
91
109
  renderer.call(record)
92
110
  end
111
+
112
+ def klass
113
+ label.parameterize.underscore
114
+ end
115
+
116
+ def ordered_by?
117
+ order_by == name.to_s && %w(asc desc).include?(order)
118
+ end
119
+
120
+ def params
121
+ view_context.params.merge(path_params)
122
+ end
123
+
124
+ def order_desc_path
125
+ if ordered_by? && order == "desc"
126
+ view_context.url_for(params.merge(order: nil, order_by: nil))
127
+ else
128
+ view_context.url_for(params.merge(order: :desc, order_by: name))
129
+ end
130
+ end
131
+
132
+ def order_asc_path
133
+ if ordered_by? && order == "asc"
134
+ view_context.url_for(params.merge(order: nil, order_by: nil))
135
+ else
136
+ view_context.url_for(params.merge(order: :asc, order_by: name))
137
+ end
138
+ end
139
+
140
+ def order_desc_class
141
+ if ordered_by? && order == "desc"
142
+ "glyphicon-triangle-bottom"
143
+ else
144
+ "glyphicon-chevron-down"
145
+ end
146
+ end
147
+
148
+ def order_asc_class
149
+ if ordered_by? && order == "asc"
150
+ "glyphicon-triangle-top"
151
+ else
152
+ "glyphicon-chevron-up"
153
+ end
154
+ end
93
155
  end
94
156
  end
95
157
 
96
158
  def greedo(scope,
97
159
  param_name: :page,
98
160
  page: params.fetch(param_name) { 1 }.to_i,
99
- per_page: 20,
161
+ per_page: nil,
162
+ path_params: {},
163
+ order: params[:order],
164
+ order_by: params[:order_by],
100
165
  &block)
101
- paginator = Paginator.build(scope, page: page, per_page: per_page)
102
- grid = Grid.new(paginator: paginator, view_context: self)
166
+ grid = Grid.new(view_context: self,
167
+ order: order,
168
+ order_by: order_by,
169
+ path_params: path_params)
103
170
  grid.configure(&block)
104
- render partial: "greedo/grid", locals: {grid: grid, param_name: param_name}
171
+ grid.paginator = Paginator.build(scope,
172
+ page: page,
173
+ per_page: (params[:per_page] || per_page || 20).to_i,
174
+ order_by: grid.ordered_by)
175
+ render partial: "greedo/grid", locals: {grid: grid,
176
+ param_name: param_name,
177
+ path_params: path_params}
105
178
  end
106
179
  end
107
180
  end
@@ -3,40 +3,59 @@ require "will_paginate-bootstrap"
3
3
 
4
4
  module Greedo
5
5
  class Paginator
6
- attr_reader :scope, :page, :per_page
6
+ attr_reader :scope, :page, :per_page, :order, :order_by
7
7
 
8
8
  def self.build(scope,
9
- params: {},
10
- page: params.fetch(:page) { 1 }.to_i,
11
- per_page: params.fetch(:per_page) { 20 }.to_i)
12
- return ArrayPaginator.new(scope) if scope.is_a?(Array)
13
- Paginator.new(scope, page: page, per_page: per_page)
9
+ page: 1,
10
+ per_page: 20,
11
+ order_by: nil)
12
+ return ArrayPaginator.new(scope, order_by.order, order_by.sort) if scope.is_a?(Array)
13
+ Paginator.new(scope, page: page, per_page: per_page,
14
+ order: order_by.order, order_by: order_by.sort)
14
15
  end
15
16
 
16
- def initialize(scope, page:, per_page:)
17
+ def initialize(scope, page:, per_page:, order: nil, order_by: nil)
17
18
  @scope = scope
18
19
  @page = page
19
20
  @per_page = per_page
21
+ @order = order
22
+ @order_by = order_by
20
23
  end
21
24
 
22
25
  def records
23
- scope.paginate(page: page, per_page: per_page)
26
+ paginated = scope.paginate(page: page, per_page: per_page)
27
+ order_by_fields = [order_by].flatten.map { |field| "#{field} #{order}" }.join(", ")
28
+ order_by ? paginated.reorder(order_by_fields) : paginated
24
29
  end
25
30
 
26
31
  def show?
27
- scope.count > per_page
32
+ scope.length > per_page
28
33
  end
29
34
  end
30
35
 
31
36
  class ArrayPaginator
32
- attr_reader :records
37
+ attr_reader :order, :order_by
33
38
 
34
- def initialize(records)
39
+ def initialize(records, order, order_by)
35
40
  @records = records
41
+ @order = order
42
+ @order_by = order_by
43
+ end
44
+
45
+ def records
46
+ @sorted_records ||= sort_records
36
47
  end
37
48
 
38
49
  def show?
39
50
  false
40
51
  end
52
+
53
+ private
54
+
55
+ def sort_records
56
+ sorted = order_by ? @records.sort_by { |r| r.send(order_by) } : @records
57
+
58
+ order == "desc" ? sorted.reverse : sorted
59
+ end
41
60
  end
42
61
  end
@@ -1,3 +1,3 @@
1
1
  module Greedo
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -13,12 +13,12 @@ RSpec.describe Greedo::GridHelper, type: :helper do
13
13
  create_project(name: "Foo")
14
14
 
15
15
  html = helper.greedo(Project.all) do |g|
16
- g.column :id, label: "ID"
17
- g.column :name
16
+ g.column :id, label: "ID", sort: false
17
+ g.column :name, sort: false
18
18
  end
19
19
 
20
- expect(html).to include("<th>ID</th>")
21
- expect(html).to include("<th>Name</th>")
20
+ expect(html).to include("ID")
21
+ expect(html).to include("Name")
22
22
  end
23
23
 
24
24
  it "renders values for each of the columns specified" do
@@ -26,8 +26,8 @@ RSpec.describe Greedo::GridHelper, type: :helper do
26
26
  bar = create_project(name: "Bar")
27
27
 
28
28
  html = helper.greedo(Project.all) do |g|
29
- g.column :id, label: "ID"
30
- g.column :name
29
+ g.column :id, label: "ID", sort: false
30
+ g.column :name, sort: false
31
31
  end
32
32
 
33
33
  expect(html).to include("Foo")
@@ -40,7 +40,7 @@ RSpec.describe Greedo::GridHelper, type: :helper do
40
40
  create_project(name: "Foo")
41
41
 
42
42
  html = helper.greedo(Project.all) do |g|
43
- g.column :name do |p|
43
+ g.column "Name" do |p|
44
44
  p.name.upcase
45
45
  end
46
46
  end
@@ -86,8 +86,8 @@ RSpec.describe Greedo::GridHelper, type: :helper do
86
86
  bar = create_project(name: "Bar")
87
87
 
88
88
  html = helper.greedo(Project.all) do |g|
89
- g.column :id
90
- g.column :name
89
+ g.column :id, sort: false
90
+ g.column :name, sort: false
91
91
  end
92
92
 
93
93
  expect(html).to have_selector("tr#project-#{foo.id}")
@@ -100,8 +100,8 @@ RSpec.describe Greedo::GridHelper, type: :helper do
100
100
 
101
101
  html = helper.greedo(Project.all) do |g|
102
102
  g.row_id { |p| "asdf-#{p.id}" }
103
- g.column :id
104
- g.column :name
103
+ g.column :id, sort: false
104
+ g.column :name, sort: false
105
105
  end
106
106
 
107
107
  expect(html).to have_selector("tr#asdf-#{foo.id}")
@@ -127,7 +127,7 @@ RSpec.describe Greedo::GridHelper, type: :helper do
127
127
 
128
128
  html = helper.greedo(Project.all) do |g|
129
129
  g.presenter ProjectPresenter
130
- g.column :author
130
+ g.column :author, sort: false
131
131
  end
132
132
 
133
133
  expect(html).to include("No Author")
@@ -141,7 +141,7 @@ RSpec.describe Greedo::GridHelper, type: :helper do
141
141
  record.name
142
142
  end
143
143
 
144
- g.column :length
144
+ g.column :length, sort: false
145
145
  end
146
146
 
147
147
  expect(html).to include("3")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: greedo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Pohorecki
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-21 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,16 +84,16 @@ dependencies:
84
84
  name: rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '4.2'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '4.2'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: sqlite3
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -223,7 +223,7 @@ homepage: https://github.com/gunpowderlabs/greedo
223
223
  licenses:
224
224
  - MIT
225
225
  metadata: {}
226
- post_install_message:
226
+ post_install_message:
227
227
  rdoc_options: []
228
228
  require_paths:
229
229
  - lib
@@ -238,9 +238,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
238
  - !ruby/object:Gem::Version
239
239
  version: '0'
240
240
  requirements: []
241
- rubyforge_project:
242
- rubygems_version: 2.4.8
243
- signing_key:
241
+ rubyforge_project:
242
+ rubygems_version: 2.7.6
243
+ signing_key:
244
244
  specification_version: 4
245
245
  summary: A very simple grid library
246
246
  test_files: