Permalink
Browse files

frill VERSION 0.1.0

* WARNING: backwards Incompatible change:
  - `helper` method changed to `helpers` and `h`
    to ease transition from draper
  • Loading branch information...
1 parent c9474f7 commit 245b9602d0a7592a4a933850891363ad2a1c7cde @moonmaster9000 committed Jul 22, 2012
Showing with 74 additions and 26 deletions.
  1. +1 −1 Gemfile.lock
  2. +1 −1 VERSION
  3. +3 −7 app/frills/view_context_frill.rb
  4. +2 −2 lib/generators/frill/templates/frill.rb
  5. +66 −14 readme.markdown
  6. +1 −1 test_frill_rails/Gemfile.lock
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- frill (0.0.2)
+ frill (0.1.0)
GEM
remote: http://rubygems.org/
View
@@ -1 +1 @@
-0.0.2
+0.1.0
@@ -4,19 +4,15 @@ module ViewContextFrill
def self.frill? object, controller
object.class_eval do
- define_method :helper do
+ define_method :helpers do
@frill_helper ||= controller.view_context
end
define_method :h do
- helper
- end
-
- define_method :helpers do
- helper
+ helpers
end
- private :helper, :h, :helpers
+ private :h, :helpers
end
false
@@ -20,9 +20,9 @@ def self.frill?(object, context)
# Decorate methods on `object`.
# If you want to use a view helper like `link_to` or a route helper like
- # `root_path`, use the `helper` method to access them:
+ # `root_path`, use the `helpers` method (or its short alias, `h`) to access them:
# def created_at
- # helper.content_tag :b, super
+ # h.content_tag :b, super
# end
end
<% end -%>
View
@@ -3,10 +3,8 @@
[![Build Status](https://secure.travis-ci.org/moonmaster9000/frill.png)](http://travis-ci.org/moonmaster9000/frill)
[![Build Dependency Status](https://gemnasium.com/moonmaster9000/frill.png)](https://gemnasium.com/moonmaster9000/frill.png)
-Simple decoration of objects for presentation. If you don't know what I'm talking
-about, read up on decorators and their role in MVC.
+Simple decoration of objects for presentation. Out of the box integration with Rails.
-Out of the box integration with Rails.
## Installation
@@ -16,58 +14,104 @@ Throw this in your Gemfile:
gem "frill"
```
+Generate a frill:
+
+```sh
+$ rails g frill Timestamp --test-framework=rspec
+ create app/frills/timestamp_frill.rb
+ invoke rspec
+ create spec/frills/timestamp_frill_spec.rb
+```
+
## Usage
(For the purposes of this tutorial, I'm going to assume you're using
`frill` inside a Rails app. Checkout the `Usage outside Rails` section
-below for information about how to use this outside of a Rails app.)
+below if you're not using Rails.)
Imagine you're creating a web application that includes both a
JSON API and an HTML frontend, and you've decided to always present a
timestamp as YEAR/MONTH/DAY. Furthermore, when presented in HTML, you
always want your timestamps wrapped in `<b>` tags.
-This is a perfect fit for the GoF decorator pattern.
+This is a perfect fit for the GoF decorator pattern. Start by generating a `Timestamp` frill:
+
+```sh
+$ rails g frill Timestamp --test-framework=rspec
+ create app/frills/timestamp_frill.rb
+ invoke rspec
+ create spec/frills/timestamp_frill_spec.rb
+```
+
+You can safely leave off the `--test-framework=rspec` portion if you've configured rspec as your default framework (or
+if you're not using rspec at all).
-Create a new TimestampFrill module in `app/frills/timestamp_frill`:
+Now open up `app/frills/timestamp_frill.rb` and format those timestamps:
```ruby
module TimestampFrill
include Frill
def self.frill? object, context
- object.respond_to? :created_at
+ object.respond_to?(:created_at) && object.respond_to?(:updated_at)
end
def created_at
- super.strftime "%Y/%m/%d"
+ format_time super
+ end
+
+ def updated_at
+ format_time super
+ end
+
+ private
+
+ def format_time(t)
+ t.strftime "%Y/%m/%d"
end
end
```
The first method `self.frill?` tells `Frill` what kind of objects this
-decorator is applicable to. In our case, it's any object that responds
-to `created_at`.
+decorator is applicable to. In our case, it's any object that have timestamps.
Next, let's create an `HtmlTimestampFrill` module:
+```sh
+$ rails g frill HtmlTimestamp --test-framework=rspec
+ create app/frills/html_timestamp_frill.rb
+ invoke rspec
+ create spec/frills/html_timestamp_frill_spec.rb
+```
+
```ruby
module HtmlTimestampFrill
include Frill
after TimestampFrill
def self.frill? object, context
- object.respond_to?(:created_at) && context.request.format.html?
+ object.respond_to?(:created_at) &&
+ object.respond_to?(:updated_at) &&
+ context.request.format.html?
end
def created_at
- helper.content_tag :b, super
+ format_time_for_html super
+ end
+
+ def updated_at
+ format_time_for_html super
+ end
+
+ private
+ def format_time_for_html t
+ h.content_tag :b, t
end
end
```
Two things to note: the `HtmlTimestampFrill` is only applicable to
-objects that respond to `created_at` when presented in "html". Also, we
+objects that have timestamps _when presented in "html"_. Also, we
tell `Frill` to decorate after `TimestampFrill` is applied (so that
`super` in `created_at` returns our `TimestampFrill` response).
@@ -109,4 +153,12 @@ Frill.decorate my_object, my_context
## License
-MIT.
+(The MIT License)
+
+Copyright © 2012 Matt Parker
+
+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:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+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.
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
- frill (0.0.2)
+ frill (0.1.0)
GEM
remote: https://rubygems.org/

0 comments on commit 245b960

Please sign in to comment.