greedo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/greedo/grid_helper.rb +16 -5
- data/lib/greedo/version.rb +1 -1
- data/spec/greedo/grid_helper_spec.rb +37 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a89bac7cd8d24f99e8f427fdfa36cacd8b1f1d5b
|
4
|
+
data.tar.gz: c13b4badf51810de63f666cc1375ea9132b9798f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ca476abd4c6d6b02a5f60db77e6eefeb99efa9695eb34ac81449c39eb5ffb31efbf17b8e402438bc9b87b1110bfa9ef7d5ed4bd52ee9408ff09d055f2efc876
|
7
|
+
data.tar.gz: 8b953f2160f9b37ca5ce3f15bb6f9eea5519fa04f7a8b4cc977876e352e82aecd35736b23b694f4c726e12e5f5121f9cbc9f786a327975cf239fdf484e5125cb
|
data/README.md
CHANGED
@@ -33,6 +33,29 @@ This will create a data table with two columns, one labelled "Name" and the othe
|
|
33
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
|
+
### Presenters
|
37
|
+
|
38
|
+
You can wrap records in a class instance:
|
39
|
+
|
40
|
+
```haml
|
41
|
+
%h1 Table with class presenters
|
42
|
+
|
43
|
+
= greedo(User.registered) do |g|
|
44
|
+
= g.presenter UserPresenter
|
45
|
+
= g.column :manager_name
|
46
|
+
```
|
47
|
+
|
48
|
+
or the same with a block:
|
49
|
+
|
50
|
+
```haml
|
51
|
+
%h1 Table with class presenters
|
52
|
+
|
53
|
+
= greedo(User.registered) do |g|
|
54
|
+
- g.presenter do |record|
|
55
|
+
UserPresenter.new(record)
|
56
|
+
= g.column :manager_name
|
57
|
+
```
|
58
|
+
|
36
59
|
## Limitations
|
37
60
|
|
38
61
|
This is a very simple helper for now - there's no sorting, or even any way to easily customize the generated HTML. This will change in time, but for now I'm open-sourcing this mainly to share this useful bit of code between projects.
|
data/lib/greedo/grid_helper.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
module Greedo
|
2
2
|
module GridHelper
|
3
3
|
class Grid
|
4
|
-
attr_reader :paginator, :view_context, :fields
|
4
|
+
attr_reader :paginator, :view_context, :fields, :presenter
|
5
5
|
|
6
|
-
def initialize(paginator
|
6
|
+
def initialize(paginator:, view_context:)
|
7
7
|
@paginator = paginator
|
8
8
|
@view_context = view_context
|
9
9
|
@row_id = ->(record) { default_row_id(record) }
|
10
10
|
@fields = []
|
11
|
+
@presenter = proc{|r| r}
|
11
12
|
end
|
12
13
|
|
13
14
|
def configure
|
@@ -21,15 +22,25 @@ module Greedo
|
|
21
22
|
|
22
23
|
def column(name, label: name.to_s.humanize, &block)
|
23
24
|
if block
|
24
|
-
renderer = ->(record) { view_context.capture(record, &block) }
|
25
|
+
renderer = ->(record) { view_context.capture(present(record), &block) }
|
25
26
|
else
|
26
|
-
renderer = ->(record) { record.public_send(name) }
|
27
|
+
renderer = ->(record) { present(record).public_send(name) }
|
27
28
|
end
|
28
29
|
|
29
30
|
fields << Field.new(name, label, renderer)
|
30
31
|
nil
|
31
32
|
end
|
32
33
|
|
34
|
+
def presenter(klass = nil, &block)
|
35
|
+
block = proc{|r| klass.new(r)} if klass
|
36
|
+
@presenter = block
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def present(record)
|
41
|
+
@presenter.call(record)
|
42
|
+
end
|
43
|
+
|
33
44
|
def rows
|
34
45
|
records.map do |record|
|
35
46
|
Row.new(record, @row_id, fields)
|
@@ -88,7 +99,7 @@ module Greedo
|
|
88
99
|
per_page: 20,
|
89
100
|
&block)
|
90
101
|
paginator = Paginator.build(scope, page: page, per_page: per_page)
|
91
|
-
grid = Grid.new(paginator, self)
|
102
|
+
grid = Grid.new(paginator: paginator, view_context: self)
|
92
103
|
grid.configure(&block)
|
93
104
|
render partial: "greedo/grid", locals: {grid: grid, param_name: param_name}
|
94
105
|
end
|
data/lib/greedo/version.rb
CHANGED
@@ -116,6 +116,43 @@ RSpec.describe Greedo::GridHelper, type: :helper do
|
|
116
116
|
expect(html).to include("No data to show.")
|
117
117
|
end
|
118
118
|
|
119
|
+
class ProjectPresenter < SimpleDelegator
|
120
|
+
def author
|
121
|
+
"No Author"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
it "wraps items in presenter" do
|
126
|
+
create_project(name: "foo")
|
127
|
+
|
128
|
+
html = helper.greedo(Project.all) do |g|
|
129
|
+
g.presenter ProjectPresenter
|
130
|
+
g.column :author
|
131
|
+
end
|
132
|
+
|
133
|
+
expect(html).to include("No Author")
|
134
|
+
end
|
135
|
+
|
136
|
+
it "wraps items in a block presenter" do
|
137
|
+
create_project(name: "foo")
|
138
|
+
|
139
|
+
html = helper.greedo(Project.all) do |g|
|
140
|
+
g.presenter do |record|
|
141
|
+
record.name
|
142
|
+
end
|
143
|
+
|
144
|
+
g.column :length
|
145
|
+
end
|
146
|
+
|
147
|
+
expect(html).to include("3")
|
148
|
+
end
|
149
|
+
|
150
|
+
it "does not pollute the output when printing the presenter output" do
|
151
|
+
helper.greedo(Project.all) do |g|
|
152
|
+
expect(g.presenter {|r| r.name}).to be_nil
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
119
156
|
def create_project(attrs = {})
|
120
157
|
Project.create!({
|
121
158
|
name: "Project #{Project.count + 1}"
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Pohorecki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|