frill 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.1
@@ -4,9 +4,15 @@ module ViewContextFrill
4
4
 
5
5
  def self.frill? object, controller
6
6
  object.class_eval do
7
- define_method :helper do
7
+ define_method :helpers do
8
8
  @frill_helper ||= controller.view_context
9
9
  end
10
+
11
+ define_method :h do
12
+ helpers
13
+ end
14
+
15
+ private :h, :helpers
10
16
  end
11
17
 
12
18
  false
data/lib/frill/engine.rb CHANGED
@@ -5,21 +5,27 @@ module Frill
5
5
  config.autoload_paths << "app/frills"
6
6
 
7
7
  initializer "frill.rails_integration" do
8
- require 'frill/rails'
8
+ ActiveSupport.on_load(:action_controller) do
9
+ require 'frill/rails'
10
+ end
11
+ end
12
+
13
+ config.after_initialize do |app|
14
+ app.config.paths.add 'app/frills', :eager_load => true
9
15
  end
10
16
 
11
17
  config.to_prepare do
12
18
  if Rails.env.development? && !Rails.application.config.cache_classes
13
19
  Frill.reset!
14
20
 
15
- Frill::Engine.force_load Dir["#{Frill::Engine.root}/app/frills/*"]
16
- Frill::Engine.force_load Dir["#{Rails.root}/app/frills/*"]
21
+ Frill::Engine.force_load Dir["#{Frill::Engine.root}/app/frills/**/*"]
22
+ Frill::Engine.force_load Dir["#{Rails.root}/app/frills/**/*"]
17
23
  end
18
24
  end
19
25
 
20
26
  def self.force_load files
21
- files.each do |f|
22
- load f
27
+ files.each do |f|
28
+ require_dependency f
23
29
  end
24
30
  end
25
31
  end
data/lib/frill/rails.rb CHANGED
@@ -1,5 +1,16 @@
1
- class ActionController::Base
2
- def frill object
3
- Frill.decorate object, self
1
+ module ActionController
2
+ class Base
3
+ helper_method :frill
4
+
5
+ private
6
+ def frill object
7
+ if object.respond_to?(:each)
8
+ object.each do |o|
9
+ Frill.decorate o, self
10
+ end
11
+ else
12
+ Frill.decorate object, self
13
+ end
14
+ end
4
15
  end
5
16
  end
@@ -0,0 +1,17 @@
1
+ module Rails
2
+ module Generators
3
+ class FrillGenerator < NamedBase
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ check_class_collision suffix: "Frill"
6
+
7
+
8
+ def create_frill_file
9
+ template 'frill.rb', File.join('app/frills/', class_path, "#{file_name}_frill.rb")
10
+ end
11
+
12
+ hook_for :test_framework
13
+
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,28 @@
1
+ <% module_namespacing do -%>
2
+ module <%= class_name %>Frill
3
+ include Frill
4
+
5
+ # Decorating before or after another frill:
6
+ # after SomeFrill
7
+ # or...
8
+ # before SomeFrill
9
+ # or...
10
+ # first
11
+
12
+ # frill? tells Frill when to decorate an object with this module.
13
+ # If you're decorating within a controller action via the `frill`
14
+ # method, then the context is the controller instance.
15
+ # This would allow you to scope this frill to only certain types
16
+ # of requests (e.g., context.request.json?)
17
+ def self.frill?(object, context)
18
+ false
19
+ end
20
+
21
+ # Decorate methods on `object`.
22
+ # If you want to use a view helper like `link_to` or a route helper like
23
+ # `root_path`, use the `helpers` method (or its short alias, `h`) to access them:
24
+ # def created_at
25
+ # h.content_tag :b, super
26
+ # end
27
+ end
28
+ <% end -%>
@@ -0,0 +1,9 @@
1
+ module Rspec
2
+ class FrillGenerator < ::Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def create_spec_file
6
+ template 'frill_spec.rb', File.join('spec/frills', "#{singular_name}_frill_spec.rb")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%=class_name%>Frill do
4
+ describe ".frill?" do
5
+ pending "replace this with a real test"
6
+ end
7
+ end
data/readme.markdown CHANGED
@@ -1,9 +1,10 @@
1
1
  # Frill
2
2
 
3
- Simple decoration of objects for presentation. If you don't know what I'm talking
4
- about, reading up on decorators and their role in MVC.
3
+ [![Build Status](https://secure.travis-ci.org/moonmaster9000/frill.png)](http://travis-ci.org/moonmaster9000/frill)
4
+ [![Build Dependency Status](https://gemnasium.com/moonmaster9000/frill.png)](https://gemnasium.com/moonmaster9000/frill.png)
5
+
6
+ Simple decoration of objects for presentation. Out of the box integration with Rails.
5
7
 
6
- Out of the box integration with Rails.
7
8
 
8
9
  ## Installation
9
10
 
@@ -13,58 +14,104 @@ Throw this in your Gemfile:
13
14
  gem "frill"
14
15
  ```
15
16
 
17
+ Generate a frill:
18
+
19
+ ```sh
20
+ $ rails g frill Timestamp --test-framework=rspec
21
+ create app/frills/timestamp_frill.rb
22
+ invoke rspec
23
+ create spec/frills/timestamp_frill_spec.rb
24
+ ```
25
+
16
26
  ## Usage
17
27
 
18
28
  (For the purposes of this tutorial, I'm going to assume you're using
19
29
  `frill` inside a Rails app. Checkout the `Usage outside Rails` section
20
- below for information about how to use this outside of a Rails app.)
30
+ below if you're not using Rails.)
21
31
 
22
32
  Imagine you're creating a web application that includes both a
23
33
  JSON API and an HTML frontend, and you've decided to always present a
24
34
  timestamp as YEAR/MONTH/DAY. Furthermore, when presented in HTML, you
25
- always want your timestamps wrapped in `<bold>` tags.
35
+ always want your timestamps wrapped in `<b>` tags.
36
+
37
+ This is a perfect fit for the GoF decorator pattern. Start by generating a `Timestamp` frill:
26
38
 
27
- This is a perfect fit for the GoF decorator pattern.
39
+ ```sh
40
+ $ rails g frill Timestamp --test-framework=rspec
41
+ create app/frills/timestamp_frill.rb
42
+ invoke rspec
43
+ create spec/frills/timestamp_frill_spec.rb
44
+ ```
45
+
46
+ You can safely leave off the `--test-framework=rspec` portion if you've configured rspec as your default framework (or
47
+ if you're not using rspec at all).
28
48
 
29
- Create a new TimestampFrill module in `app/frills/timestamp_frill`:
49
+ Now open up `app/frills/timestamp_frill.rb` and format those timestamps:
30
50
 
31
51
  ```ruby
32
52
  module TimestampFrill
33
53
  include Frill
34
54
 
35
55
  def self.frill? object, context
36
- object.respond_to? :created_at
56
+ object.respond_to?(:created_at) && object.respond_to?(:updated_at)
37
57
  end
38
58
 
39
59
  def created_at
40
- super.strftime "%Y/%m/%d"
60
+ format_time super
61
+ end
62
+
63
+ def updated_at
64
+ format_time super
65
+ end
66
+
67
+ private
68
+
69
+ def format_time(t)
70
+ t.strftime "%Y/%m/%d"
41
71
  end
42
72
  end
43
73
  ```
44
74
 
45
75
  The first method `self.frill?` tells `Frill` what kind of objects this
46
- decorator is applicable to. In our case, it's any object that responds
47
- to `created_at`.
76
+ decorator is applicable to. In our case, it's any object that have timestamps.
48
77
 
49
78
  Next, let's create an `HtmlTimestampFrill` module:
50
79
 
80
+ ```sh
81
+ $ rails g frill HtmlTimestamp --test-framework=rspec
82
+ create app/frills/html_timestamp_frill.rb
83
+ invoke rspec
84
+ create spec/frills/html_timestamp_frill_spec.rb
85
+ ```
86
+
51
87
  ```ruby
52
88
  module HtmlTimestampFrill
53
89
  include Frill
54
90
  after TimestampFrill
55
91
 
56
92
  def self.frill? object, context
57
- object.respond_to?(:created_at) && context.request.format == "text/html"
93
+ object.respond_to?(:created_at) &&
94
+ object.respond_to?(:updated_at) &&
95
+ context.request.format.html?
58
96
  end
59
97
 
60
98
  def created_at
61
- helper.content_tag :b, super
99
+ format_time_for_html super
100
+ end
101
+
102
+ def updated_at
103
+ format_time_for_html super
104
+ end
105
+
106
+ private
107
+ def format_time_for_html t
108
+ h.content_tag :b, t
62
109
  end
63
110
  end
64
111
  ```
65
112
 
66
113
  Two things to note: the `HtmlTimestampFrill` is only applicable to
67
- objects that respond to `created_at` when presented in "html". Also, we
114
+ objects that have timestamps _when presented in "html"_. Also, we
68
115
  tell `Frill` to decorate after `TimestampFrill` is applied (so that
69
116
  `super` in `created_at` returns our `TimestampFrill` response).
70
117
 
@@ -91,6 +138,31 @@ In your html view, you simply call `@article.created_at`:
91
138
 
92
139
  The same goes for your JSON view.
93
140
 
141
+ ### 'frill' decorates individual objects _and_ collections
142
+
143
+ The `frill` helper will decorate both collections and associations. You can use it both within your controller
144
+ and within your views.
145
+
146
+ For example, inside a controller:
147
+
148
+ ```ruby
149
+ class PostsController < ApplicationController
150
+ def index
151
+ @posts = frill Post.all
152
+ end
153
+
154
+ def show
155
+ @post = frill Post.find(params[:id])
156
+ end
157
+ end
158
+ ```
159
+
160
+ Or, in a view:
161
+
162
+ ```erb
163
+ <%= render frill(@post.comments) %>
164
+ ```
165
+
94
166
  ## Usage outside Rails
95
167
 
96
168
  There are really just two integrations in a Rails app: the `frill`
@@ -106,4 +178,12 @@ Frill.decorate my_object, my_context
106
178
 
107
179
  ## License
108
180
 
109
- MIT.
181
+ (The MIT License)
182
+
183
+ Copyright © 2012 Matt Parker
184
+
185
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
186
+
187
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
188
+
189
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.0.2
4
+ version: 0.1.1
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-04-30 00:00:00.000000000 Z
12
+ date: 2012-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70197025256920 !ruby/object:Gem::Requirement
16
+ requirement: &70317754031900 !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: *70197025256920
24
+ version_requirements: *70317754031900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70197025254800 !ruby/object:Gem::Requirement
27
+ requirement: &70317754031320 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,29 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70197025254800
35
+ version_requirements: *70317754031320
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-rails
38
+ requirement: &70317754030280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70317754030280
47
+ - !ruby/object:Gem::Dependency
48
+ name: capybara
49
+ requirement: &70317754029580 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70317754029580
36
58
  description:
37
59
  email: moonmaster9000@gmail.com
38
60
  executables: []
@@ -43,6 +65,10 @@ files:
43
65
  - lib/frill/frill.rb
44
66
  - lib/frill/rails.rb
45
67
  - lib/frill.rb
68
+ - lib/generators/frill/frill_generator.rb
69
+ - lib/generators/frill/templates/frill.rb
70
+ - lib/generators/rspec/frill_generator.rb
71
+ - lib/generators/rspec/templates/frill_spec.rb
46
72
  - app/frills/view_context_frill.rb
47
73
  - VERSION
48
74
  - readme.markdown