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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3640b00c249ab0f3ec46c9effc6ea1a760c65c6
4
- data.tar.gz: 7a527cd406993f475c47b08473f2cec9c18dcbae
3
+ metadata.gz: a89bac7cd8d24f99e8f427fdfa36cacd8b1f1d5b
4
+ data.tar.gz: c13b4badf51810de63f666cc1375ea9132b9798f
5
5
  SHA512:
6
- metadata.gz: 642c57b5c69b8383670ad28aeea10255701c63f68d709b5b57b8f9ffe6f7dbfcd541b4b7e2c2ed842e8d1cc25a22477982a2d1ed6b6b719171814001617e72ea
7
- data.tar.gz: bee6f1092985ca86be8e7bb09648324dd18bff12df13e2ad3bbc0bf413580e6f7aa0b2f0b302176e5cbb25d5a29a02f10cada07c864768a1355cbfc4b1f2d32f
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.
@@ -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, view_context)
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
@@ -1,3 +1,3 @@
1
1
  module Greedo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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.1
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-18 00:00:00.000000000 Z
11
+ date: 2015-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler