rails_decorators 0.1.0

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/README.markdown ADDED
@@ -0,0 +1,56 @@
1
+ Rails Decorators
2
+ ================
3
+
4
+ This gem makes it easy to apply the decorator pattern to the models in a Rails application.
5
+
6
+ ## Why?
7
+
8
+ Helpers, as they're commonly used, are a bit odd. In both Ruby and Rails we approach everything from an Object-Oriented perspective, then with helpers we get procedural.
9
+
10
+ The job of a helper is to take in data or a data object and output presentation-ready results. We can do that job in an OO fashion with a decorator.
11
+
12
+ In general, a decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then a decorator might add instance methods like `.formatted_published_at` or `.formatted_title` that output actual HTML.
13
+
14
+ ## How?
15
+
16
+ Here are the steps to utilizing this pattern:
17
+
18
+ Add the dependency to your `Gemfile`:
19
+
20
+ ```
21
+ gem "rails_decorators"
22
+ ```
23
+
24
+ Run bundle:
25
+
26
+ ```
27
+ bundle
28
+ ```
29
+
30
+ Run the setup generator to create a decorators folder and an initializer to connect your decorators with your models:
31
+
32
+ ```
33
+ rails generate decorator:setup
34
+ ```
35
+
36
+ Create a decorator for your model (ex: `Article`)
37
+
38
+ ```
39
+ rails generate decorator:model Article
40
+ ```
41
+
42
+ Open the decorator module (ex: `app/decorators/article_decorator.rb`)
43
+
44
+ Add your new formatting methods inside the `instance_methods` module
45
+
46
+ If you need access to the Rails helpers like `link_to` and `content_tag`, include the appropriate modules (commented out in the generated decorator)
47
+
48
+ Use the new methods in your views like any other model method (ex: `@article.formatted_published_at`)
49
+
50
+ ## Uses
51
+
52
+ Here are some ideas of what you might do in decorator methods:
53
+
54
+ * Implement output formatting for `to_csv`, `to_json`, or `to_xml`
55
+ * Format dates and times using `strftime`
56
+ * Implement a commonly used representation of the data object like a `.name` method that combines `first_name` and `last_name` attributes
@@ -0,0 +1,8 @@
1
+ Description:
2
+ The decorator:model generator creates a decorator module in /app/decorators
3
+ setup to interact with the named model.
4
+
5
+ Examples:
6
+ rails generate decorator:model Article
7
+
8
+ file: app/decorators/article_decorator.rb
@@ -0,0 +1,9 @@
1
+ module Decorator
2
+ class ModelGenerator < Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def build_module
6
+ template 'module.rb', "app/decorators/#{singular_name}_decorator.rb"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module <%= singular_name.camelize %>Decorator
2
+ extend ActiveSupport::Concern
3
+
4
+ # Uncomment this line to have access to the Rails view helpers like link_to and content_tag:
5
+ # include ActionView::Helpers
6
+
7
+ module InstanceMethods
8
+ # Add your instance methods here
9
+
10
+ # Example:
11
+ # def formatted_created_at
12
+ # content_tag :span, created_at.strftime("%A")
13
+ # end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ Description:
2
+ The setup generator creates an /app/decorators folder and
3
+ a /config/initializers/load_decorators.rb initializer.
4
+
5
+ Any decorator module in /app/decorators that matches a model name
6
+ will be included into that model.
7
+
8
+ Examples:
9
+ rails generate decorators:setup
10
+
11
+ Folder: app/decorators
12
+ Initializer: config/initializers/load_decorators.rb
@@ -0,0 +1,15 @@
1
+ module Decorator
2
+ class SetupGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def create_directory
6
+ empty_directory "app/decorators"
7
+ end
8
+
9
+ def build_initializer
10
+ initializer("load_decorators.rb") do
11
+ "RailsDecorators::Loader.load"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require 'rails_decorators/loader'
@@ -0,0 +1,22 @@
1
+ module RailsDecorators
2
+ class Loader
3
+ def self.load
4
+ ActionController::Dispatcher.to_prepare do
5
+ models = Dir.glob(File.join(Rails.root, '/app/models/*.rb')).map do |file|
6
+ file.match(/(\w*).rb/)[1].split('_').map do |class_part|
7
+ class_part.capitalize
8
+ end.join('')
9
+ end
10
+
11
+ models.each do |model|
12
+ begin
13
+ decorator = (model + "Decorator").constantize
14
+ model.constantize.send :include, decorator
15
+ rescue NameError
16
+ # No Decorator Exists
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_decorators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Casimir
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-28 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec-rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 2.0.1
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: Rails Decorators reimagines the role of helpers in the view layer of a Rails application, allowing an object-oriented approach rather than procedural.
28
+ email: jeff@jumpstartlab.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - lib/generators/decorator/model/model_generator.rb
37
+ - lib/generators/decorator/model/templates/module.rb
38
+ - lib/generators/decorator/model/USAGE
39
+ - lib/generators/decorator/setup/setup_generator.rb
40
+ - lib/generators/decorator/setup/USAGE
41
+ - lib/rails_decorators/loader.rb
42
+ - lib/rails_decorators.rb
43
+ - README.markdown
44
+ has_rdoc: true
45
+ homepage: http://github.com/jcasimir/rails_decorators
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.4
65
+ requirements: []
66
+
67
+ rubyforge_project: rails_decorators
68
+ rubygems_version: 1.6.2
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Decorator pattern implmentation for Rails.
72
+ test_files: []
73
+