greedo 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -1
- data/app/views/greedo/_grid.html.haml +11 -2
- data/greedo.gemspec +1 -1
- data/lib/greedo/grid_helper.rb +72 -11
- data/lib/greedo/paginator.rb +28 -8
- data/lib/greedo/version.rb +1 -1
- data/spec/greedo/grid_helper_spec.rb +13 -13
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c37eaa9ca950940e9c87f9ed42e5954d03d06e4
|
4
|
+
data.tar.gz: e5c83b75858b499a110a9ac15aef2ff973c0083e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68a84e842d0191ad79765e733ee98d2283b8ec1e71055437e881de2b57a770d1a66b0638737f3bc0aba681e4d4ff95c6289ae1a41ed722c661b9f87e34bf32d7
|
7
|
+
data.tar.gz: 6a4d333a874b4228d1955e55568c6b4a652c7c64b65a976dfa8998801dd365181a33961d1ed0a06261ac2a60341eaed0d8028797c9d6df8d3db5081e53b7dabb
|
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,22 @@
|
|
5
5
|
%thead
|
6
6
|
%tr
|
7
7
|
- grid.headers.each do |header|
|
8
|
-
%th
|
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
|
22
|
+
%td{class: column.klass}
|
23
|
+
= column.value
|
15
24
|
|
16
25
|
- if grid.show_pagination?
|
17
26
|
= will_paginate grid.paginator.records, param_name: param_name, renderer: BootstrapPagination::Rails
|
data/greedo.gemspec
CHANGED
@@ -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"
|
data/lib/greedo/grid_helper.rb
CHANGED
@@ -1,33 +1,49 @@
|
|
1
1
|
module Greedo
|
2
2
|
module GridHelper
|
3
3
|
class Grid
|
4
|
-
|
4
|
+
attr_accessor :paginator
|
5
|
+
attr_reader :view_context, :fields, :presenter, :empty_message_text,
|
6
|
+
:order, :order_by
|
5
7
|
|
6
|
-
def initialize(
|
7
|
-
@paginator = paginator
|
8
|
+
def initialize(view_context:, order: nil, order_by: 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
|
12
16
|
end
|
13
17
|
|
14
18
|
def configure
|
15
19
|
yield self if block_given?
|
16
20
|
end
|
17
21
|
|
22
|
+
def custom_empty_message(empty_message_text)
|
23
|
+
@empty_message_text = empty_message_text
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def ordered_by
|
28
|
+
fields.find(&:ordered_by?) || Field.new
|
29
|
+
end
|
30
|
+
|
18
31
|
def row_id(&block)
|
19
32
|
@row_id = block
|
20
33
|
nil
|
21
34
|
end
|
22
35
|
|
23
|
-
def column(name, label: name.to_s.humanize, &block)
|
36
|
+
def column(name, label: name.to_s.humanize, sort: nil, &block)
|
37
|
+
if sort.nil? && name.is_a?(Symbol)
|
38
|
+
sort = name.to_s
|
39
|
+
end
|
24
40
|
if block
|
25
41
|
renderer = ->(record) { view_context.capture(present(record), &block) }
|
26
42
|
else
|
27
43
|
renderer = ->(record) { present(record).public_send(name) }
|
28
44
|
end
|
29
45
|
|
30
|
-
fields << Field.new(name, label, renderer)
|
46
|
+
fields << Field.new(name, label, renderer, order, order_by, view_context, sort)
|
31
47
|
nil
|
32
48
|
end
|
33
49
|
|
@@ -60,7 +76,7 @@ module Greedo
|
|
60
76
|
end
|
61
77
|
|
62
78
|
def empty_message
|
63
|
-
view_context.content_tag(:p) {
|
79
|
+
view_context.content_tag(:p) { empty_message_text }
|
64
80
|
end
|
65
81
|
|
66
82
|
private
|
@@ -74,7 +90,7 @@ module Greedo
|
|
74
90
|
"#{snake_class}-#{record.id}"
|
75
91
|
end
|
76
92
|
|
77
|
-
Column = Struct.new(:value)
|
93
|
+
Column = Struct.new(:value, :klass)
|
78
94
|
|
79
95
|
Row = Struct.new(:record, :row_id, :fields) do
|
80
96
|
def id
|
@@ -82,14 +98,54 @@ module Greedo
|
|
82
98
|
end
|
83
99
|
|
84
100
|
def columns
|
85
|
-
fields.map{ |f| Column.new(f.value(record)) }
|
101
|
+
fields.map{ |f| Column.new(f.value(record), f.klass) }
|
86
102
|
end
|
87
103
|
end
|
88
104
|
|
89
|
-
Field = Struct.new(:name, :label, :renderer) do
|
105
|
+
Field = Struct.new(:name, :label, :renderer, :order, :order_by, :view_context, :sort) do
|
90
106
|
def value(record)
|
91
107
|
renderer.call(record)
|
92
108
|
end
|
109
|
+
|
110
|
+
def klass
|
111
|
+
label.parameterize.underscore
|
112
|
+
end
|
113
|
+
|
114
|
+
def ordered_by?
|
115
|
+
order_by == name.to_s && %w(asc desc).include?(order)
|
116
|
+
end
|
117
|
+
|
118
|
+
def order_desc_path
|
119
|
+
if ordered_by? && order == "desc"
|
120
|
+
view_context.url_for(view_context.params)
|
121
|
+
else
|
122
|
+
view_context.url_for(view_context.params.merge(order: :desc, order_by: name))
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def order_asc_path
|
127
|
+
if ordered_by? && order == "asc"
|
128
|
+
view_context.url_for(view_context.params)
|
129
|
+
else
|
130
|
+
view_context.url_for(view_context.params.merge(order: :asc, order_by: name))
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def order_desc_class
|
135
|
+
if ordered_by? && order == "desc"
|
136
|
+
"glyphicon-triangle-bottom"
|
137
|
+
else
|
138
|
+
"glyphicon-chevron-down"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def order_asc_class
|
143
|
+
if ordered_by? && order == "asc"
|
144
|
+
"glyphicon-triangle-top"
|
145
|
+
else
|
146
|
+
"glyphicon-chevron-up"
|
147
|
+
end
|
148
|
+
end
|
93
149
|
end
|
94
150
|
end
|
95
151
|
|
@@ -97,10 +153,15 @@ module Greedo
|
|
97
153
|
param_name: :page,
|
98
154
|
page: params.fetch(param_name) { 1 }.to_i,
|
99
155
|
per_page: 20,
|
156
|
+
order: params[:order],
|
157
|
+
order_by: params[:order_by],
|
100
158
|
&block)
|
101
|
-
|
102
|
-
grid = Grid.new(paginator: paginator, view_context: self)
|
159
|
+
grid = Grid.new(view_context: self, order: order, order_by: order_by)
|
103
160
|
grid.configure(&block)
|
161
|
+
grid.paginator = Paginator.build(scope,
|
162
|
+
page: page,
|
163
|
+
per_page: per_page,
|
164
|
+
order_by: grid.ordered_by)
|
104
165
|
render partial: "greedo/grid", locals: {grid: grid, param_name: param_name}
|
105
166
|
end
|
106
167
|
end
|
data/lib/greedo/paginator.rb
CHANGED
@@ -3,24 +3,30 @@ 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
9
|
params: {},
|
10
10
|
page: params.fetch(:page) { 1 }.to_i,
|
11
|
-
per_page: params.fetch(:per_page) { 20 }.to_i
|
12
|
-
|
13
|
-
|
11
|
+
per_page: params.fetch(:per_page) { 20 }.to_i,
|
12
|
+
order_by: nil)
|
13
|
+
return ArrayPaginator.new(scope, order_by.order, order_by.sort) if scope.is_a?(Array)
|
14
|
+
Paginator.new(scope, page: page, per_page: per_page,
|
15
|
+
order: order_by.order, order_by: order_by.sort)
|
14
16
|
end
|
15
17
|
|
16
|
-
def initialize(scope, page:, per_page:)
|
18
|
+
def initialize(scope, page:, per_page:, order: nil, order_by: nil)
|
17
19
|
@scope = scope
|
18
20
|
@page = page
|
19
21
|
@per_page = per_page
|
22
|
+
@order = order
|
23
|
+
@order_by = order_by
|
20
24
|
end
|
21
25
|
|
22
26
|
def records
|
23
|
-
scope.paginate(page: page, per_page: per_page)
|
27
|
+
paginated = scope.paginate(page: page, per_page: per_page)
|
28
|
+
order_by_fields = [order_by].flatten.map { |field| "#{field} #{order}" }.join(", ")
|
29
|
+
order_by ? paginated.reorder(order_by_fields) : paginated
|
24
30
|
end
|
25
31
|
|
26
32
|
def show?
|
@@ -29,14 +35,28 @@ module Greedo
|
|
29
35
|
end
|
30
36
|
|
31
37
|
class ArrayPaginator
|
32
|
-
attr_reader :
|
38
|
+
attr_reader :order, :order_by
|
33
39
|
|
34
|
-
def initialize(records)
|
40
|
+
def initialize(records, order, order_by)
|
35
41
|
@records = records
|
42
|
+
@order = order
|
43
|
+
@order_by = order_by
|
44
|
+
end
|
45
|
+
|
46
|
+
def records
|
47
|
+
@sorted_records ||= sort_records
|
36
48
|
end
|
37
49
|
|
38
50
|
def show?
|
39
51
|
false
|
40
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def sort_records
|
57
|
+
sorted = order_by ? @records.sort_by { |r| r.send(order_by) } : @records
|
58
|
+
|
59
|
+
order == "desc" ? sorted.reverse : sorted
|
60
|
+
end
|
41
61
|
end
|
42
62
|
end
|
data/lib/greedo/version.rb
CHANGED
@@ -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("
|
21
|
-
expect(html).to include("
|
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
|
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.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Pohorecki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-17 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: '
|
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: '
|
96
|
+
version: '4.2'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: sqlite3
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -239,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
239
239
|
version: '0'
|
240
240
|
requirements: []
|
241
241
|
rubyforge_project:
|
242
|
-
rubygems_version: 2.
|
242
|
+
rubygems_version: 2.6.12
|
243
243
|
signing_key:
|
244
244
|
specification_version: 4
|
245
245
|
summary: A very simple grid library
|