frill 0.1.9 → 0.1.10
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.
- data/VERSION +1 -1
- data/lib/frill/frill.rb +8 -8
- data/readme.markdown +70 -14
- data/spec/frill_spec.rb +12 -12
- metadata +14 -13
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.10
|
data/lib/frill/frill.rb
CHANGED
@@ -2,17 +2,17 @@ module Frill
|
|
2
2
|
class CyclicDependency < RuntimeError; end
|
3
3
|
|
4
4
|
def self.included(base)
|
5
|
-
self.
|
5
|
+
self.dependency_graph.add base
|
6
6
|
base.extend ClassMethods
|
7
7
|
end
|
8
8
|
|
9
9
|
def self.decorators
|
10
|
-
@decorators ||=
|
10
|
+
@decorators ||= dependency_graph.to_a
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.reset!
|
14
14
|
@decorators = nil
|
15
|
-
@
|
15
|
+
@dependency_graph = nil
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.decorate object, context
|
@@ -23,21 +23,21 @@ module Frill
|
|
23
23
|
object
|
24
24
|
end
|
25
25
|
|
26
|
-
def self.
|
27
|
-
@
|
26
|
+
def self.dependency_graph
|
27
|
+
@dependency_graph ||= DependencyGraph.new
|
28
28
|
end
|
29
29
|
|
30
30
|
module ClassMethods
|
31
31
|
def before decorator
|
32
|
-
Frill.
|
32
|
+
Frill.dependency_graph.move_before self, decorator
|
33
33
|
end
|
34
34
|
|
35
35
|
def after decorator
|
36
|
-
Frill.
|
36
|
+
Frill.dependency_graph.move_before decorator, self
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
class
|
40
|
+
class DependencyGraph
|
41
41
|
def initialize
|
42
42
|
@nodes = {}
|
43
43
|
end
|
data/readme.markdown
CHANGED
@@ -14,16 +14,67 @@ Throw this in your Gemfile:
|
|
14
14
|
gem "frill"
|
15
15
|
```
|
16
16
|
|
17
|
-
|
17
|
+
## In a nutshell
|
18
|
+
|
19
|
+
Decorate your objects for presentation.
|
18
20
|
|
19
21
|
```sh
|
20
|
-
$ rails g frill
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
$ rails g frill FooFrill
|
23
|
+
```
|
24
|
+
|
25
|
+
Frills are just modules that decorate your objects with extra functionality. The `frill?` method on the module tells `Frill` when
|
26
|
+
to decorate an object with a module:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
module FooFrill
|
30
|
+
def self.frill? object, context
|
31
|
+
object.respond_to?(:foo)
|
32
|
+
end
|
33
|
+
|
34
|
+
def foo
|
35
|
+
h.render partial: "shared/foo", locals: { foo: "#{super} bar" }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Opt objects in your controllers into frill with the `frill` method:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
class FooController < ApplicationController
|
44
|
+
def foo
|
45
|
+
@foo = frill Foo.find(params[:id])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Then just use the `foo` method on your Foo objects to get their decorated functionality:
|
51
|
+
|
52
|
+
```erb
|
53
|
+
Awesome foo page!
|
54
|
+
|
55
|
+
<%=@foo.foo%>
|
56
|
+
```
|
57
|
+
|
58
|
+
Instead of manually opting objects into decoration via the `frill` method, you can have all of your controller
|
59
|
+
instance variables automatically decorated via the `auto_frill` macro:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class ApplicationController < ActionController::Base
|
63
|
+
auto_frill
|
64
|
+
end
|
24
65
|
```
|
25
66
|
|
26
|
-
|
67
|
+
Now you don't need to use the `frill` method to decorate objects. They'll be automatically decorated before being passed off to your view.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class FooController < ApplicationController
|
71
|
+
def foo
|
72
|
+
@foo = Foo.find params[:id]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
## A longer story
|
27
78
|
|
28
79
|
Your product manager writes the following story for you:
|
29
80
|
|
@@ -39,7 +90,7 @@ Feature: Consistent Timestamp Presentation
|
|
39
90
|
```
|
40
91
|
|
41
92
|
You see this and roll your eyes. You're thinking about all of the places that you show `created_at`
|
42
|
-
timestamps on the site.
|
93
|
+
timestamps on the site. Reluctantly, you roll up your sleeves and start by writing the following helper and partial:
|
43
94
|
|
44
95
|
```ruby
|
45
96
|
module ApplicationHelper
|
@@ -64,7 +115,7 @@ You hate this approach.
|
|
64
115
|
|
65
116
|
1. It's tedious
|
66
117
|
1. It's procedural
|
67
|
-
1. Developers have to remember to manually wrap timestamps with your `format_timestamp` helper.
|
118
|
+
1. Developers have to remember to manually wrap timestamps with your `format_timestamp` helper.
|
68
119
|
|
69
120
|
After you deliver the story, your product owner says "Great! But what about the format of timestamps in the JSON api? Here's another story."
|
70
121
|
|
@@ -95,7 +146,7 @@ module ApplicationHelper
|
|
95
146
|
end
|
96
147
|
```
|
97
148
|
|
98
|
-
And now you begin the tedious
|
149
|
+
And now you begin the tedious task of updating all of the JSON views with the helper:
|
99
150
|
|
100
151
|
```ruby
|
101
152
|
json.created_at format_timestamp(@article.created_at)
|
@@ -143,10 +194,11 @@ module HtmlTimestampFrill
|
|
143
194
|
end
|
144
195
|
```
|
145
196
|
|
146
|
-
There's
|
197
|
+
There's three important things to note:
|
147
198
|
|
148
199
|
1. This frill comes after `TimestampFrill`. That tells `Frill` that it should only attempt to extend an object with this module after attempting to extend it with `TimestampFrill`.
|
149
200
|
1. The `frill?` method only returns true if it's an HTML request, meaning this frill won't be extended onto objects for your JSON api.
|
201
|
+
1. The `h` method gives you access to all of the normal view helper methods you expect to you use inside your views. You can also use `helpers`.
|
150
202
|
|
151
203
|
Lastly, opt objects into frilling inside your controllers:
|
152
204
|
|
@@ -164,7 +216,7 @@ end
|
|
164
216
|
And that's it. You don't have to update any of your views. Why? When you call the `frill` method inside your controller and pass it an object (or a collection of objects),
|
165
217
|
frill will attempt to extend the object with any applicable frills (i.e., frills that return `true` for the `frill?` method when passed the object and the request context).
|
166
218
|
|
167
|
-
That way, you can
|
219
|
+
That way, you can simply render your `created_at` attributes without any helpers, and they will automatically present themselves appropriately for their context (e.g., HTML v. JSON requests).
|
168
220
|
|
169
221
|
Note that if prefer, you can configure your controllers to automatically frill all objects for presentation by calling the `auto_frill` method inside your `ApplicationController`, instead of manually having to opt them it via the `frill` method:
|
170
222
|
|
@@ -216,9 +268,9 @@ Or, in a view:
|
|
216
268
|
|
217
269
|
## Usage outside Rails
|
218
270
|
|
219
|
-
There are really just
|
220
|
-
method inside of your controller,
|
221
|
-
helper methods inside of your module methods.
|
271
|
+
There are really just three integrations in a Rails app: the `frill`
|
272
|
+
method inside of your controller, the `auto_frill` macro for controllers,
|
273
|
+
plus the ability to call view helper methods inside of your module methods.
|
222
274
|
|
223
275
|
To kickoff the decoration of an object outside of a Rails application,
|
224
276
|
simply call `Frill.decorate`:
|
@@ -231,3 +283,7 @@ Frill.decorate my_object, my_context
|
|
231
283
|
|
232
284
|
* Ben Moss
|
233
285
|
* Nicholas Greenfield
|
286
|
+
|
287
|
+
## License
|
288
|
+
|
289
|
+
MIT
|
data/spec/frill_spec.rb
CHANGED
@@ -70,7 +70,7 @@ describe Frill do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
describe ".before" do
|
73
|
-
it "inserts the current module before the requested module in Frill's
|
73
|
+
it "inserts the current module before the requested module in Frill's dependency_graph of decorators" do
|
74
74
|
Module4.before Module3
|
75
75
|
Module2.before Module1
|
76
76
|
Module5.after Module4
|
@@ -92,20 +92,20 @@ describe Frill do
|
|
92
92
|
end
|
93
93
|
|
94
94
|
describe ".before" do
|
95
|
-
it "inserts the current module before the requested module in Frill's
|
96
|
-
Frill.
|
95
|
+
it "inserts the current module before the requested module in Frill's dependency_graph of decorators" do
|
96
|
+
Frill.dependency_graph.to_a.should == [Module1, Module2, Module3]
|
97
97
|
|
98
98
|
Module1.before Module2
|
99
|
-
Frill.
|
99
|
+
Frill.dependency_graph.to_a.should == [Module1, Module2, Module3]
|
100
100
|
|
101
101
|
Module3.before Module2
|
102
|
-
Frill.
|
102
|
+
Frill.dependency_graph.to_a.should == [Module3, Module1, Module2]
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
106
|
describe ".after" do
|
107
|
-
it "inserts the current module after the requested module in Frill's
|
108
|
-
Frill.
|
107
|
+
it "inserts the current module after the requested module in Frill's dependency_graph of decorators" do
|
108
|
+
Frill.dependency_graph.to_a.should == [Module1, Module2, Module3]
|
109
109
|
|
110
110
|
Module1.after Module2
|
111
111
|
Module3.after Module2
|
@@ -116,10 +116,10 @@ describe Frill do
|
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
describe Frill::
|
119
|
+
describe Frill::DependencyGraph do
|
120
120
|
describe "#add" do
|
121
|
-
it "should add an element to the
|
122
|
-
g = Frill::
|
121
|
+
it "should add an element to the dependency_graph" do
|
122
|
+
g = Frill::DependencyGraph.new
|
123
123
|
g.add "hi"
|
124
124
|
g["hi"].should_not be_nil
|
125
125
|
end
|
@@ -127,7 +127,7 @@ describe Frill do
|
|
127
127
|
|
128
128
|
describe "#move_before(label1, label2)" do
|
129
129
|
it "should move label1 before label2" do
|
130
|
-
g = Frill::
|
130
|
+
g = Frill::DependencyGraph.new
|
131
131
|
g.move_before "a", "b"
|
132
132
|
g.move_before "c", "d"
|
133
133
|
g.move_before "c", "b"
|
@@ -135,7 +135,7 @@ describe Frill do
|
|
135
135
|
end
|
136
136
|
|
137
137
|
it "should throw exceptions when cycles are detected" do
|
138
|
-
g = Frill::
|
138
|
+
g = Frill::DependencyGraph.new
|
139
139
|
g.move_before "c", "b"
|
140
140
|
g.move_before "b", "a"
|
141
141
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: frill
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70295008464380 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.2.2
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70295008464380
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70295008463160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70295008463160
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec-rails
|
38
|
-
requirement: &
|
38
|
+
requirement: &70295008458700 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70295008458700
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: capybara
|
49
|
-
requirement: &
|
49
|
+
requirement: &70295008457520 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70295008457520
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: pry
|
60
|
-
requirement: &
|
60
|
+
requirement: &70295008456620 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70295008456620
|
69
69
|
description:
|
70
70
|
email: moonmaster9000@gmail.com
|
71
71
|
executables: []
|
@@ -86,7 +86,8 @@ files:
|
|
86
86
|
- LICENSE
|
87
87
|
- spec/frill_spec.rb
|
88
88
|
homepage: https://github.com/moonmaster9000/frill
|
89
|
-
licenses:
|
89
|
+
licenses:
|
90
|
+
- MIT
|
90
91
|
post_install_message:
|
91
92
|
rdoc_options: []
|
92
93
|
require_paths:
|