be9-viewtastic 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ 0.1.2 - Holy smokes!
2
+ ====================
3
+
4
+ * FIX: properly add "app/presenters" to load_path
5
+ * FIX: Include Viewtastic::Activation in ActionController::Base to activate Viewtastic
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Istvan Hoka
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Viewtastic #
2
+
3
+ ## About this fork
4
+
5
+
6
+ This is the viewtastic for Rails 3. Original one, intended for 2.x, can be found [here](http://github.com/ihoka/viewtastic).
7
+
8
+ ## Installation ##
9
+
10
+ Install the gem
11
+
12
+ gem install viewtastic
13
+
14
+ Load the gem in your `environment.rb` file
15
+
16
+ config.gem "viewtastic"
17
+
18
+ ## Usage ##
19
+
20
+ My presenters go into the `app/presenters` directory of the application so this is added to the load_path in Rails by default by Viewtastic.
21
+
22
+ ### Presenters ###
23
+
24
+ A Presenter inherits from `Viewtastic::Base` and should use the `presents` method to declare presented objects.
25
+
26
+ class CommentPresenter < Viewtastic::Base
27
+ presents :comment
28
+ end
29
+
30
+ This gives you several 'magic' methods:
31
+
32
+ * All attributes of comment with the prefix 'comment'. For instance: `comment_body`, `comment_post`, `comment_created_at`.
33
+ * `comment_dom_id` is the same as calling `dom_id(comment)` in a view.
34
+
35
+ If you want to skip the prefix and just have the attribute name, you can declare:
36
+
37
+ presents :comment => [:body, :created_at]
38
+
39
+ and you get `presenter.body` and `presenter.created_at`.
40
+
41
+
42
+ Assuming you have a `Comment` model and your controller has a helper method `current_user` that returns the user currently logged in, you could make the following presenter to help in presenting products.
43
+
44
+ class CommentPresenter < Viewtastic::Base
45
+ presents :comment
46
+
47
+ def dom_id
48
+ comment_dom_id
49
+ end
50
+
51
+ def owner?
52
+ controller.current_user.comments.include?(comment)
53
+ end
54
+
55
+ def links
56
+ returning([]) do |links|
57
+ links << link_to("Edit", [:edit, comment]) if owner?
58
+ links << link_to("Reply", [:new, :comment]) if controller.current_user
59
+ end
60
+ end
61
+ end
62
+
63
+ ### Convenience ###
64
+
65
+ `each_with_presenter` is available on any `Array`, and it is designed to reuse a single Presenter instance and pass every element in the array as the presented object.
66
+
67
+ In your view (maybe `posts/show.html.erb`):
68
+
69
+ <ul>
70
+ <% @post.comments.each_with_presenter(CommentPresenter, :comment) do |comment| %>
71
+ <li id="<%= comment.dom_id %>">
72
+ <%= comment.body %>
73
+ <%= comment.links %>
74
+ </li>
75
+ <% end %>
76
+ </ul>
77
+
78
+ ## Credits ##
79
+
80
+ * [ActivePresenter](http://github.com/giraffesoft/active_presenter) was the inspiration for this project and some of the presenter code was used from ActivePresenter.
81
+ * [Authlogic](http://github.com/binarylogic/authlogic) -- the Authlogic activation code is used to activate Viewtastic on each request.
82
+
83
+ ## License ##
84
+
85
+
86
+ Copyright (c) 2009 Istvan Hoka, released under the MIT license
@@ -0,0 +1,13 @@
1
+ class Array
2
+ # Wrap each element in the Array with a presenter specified by @presenter_class@ and
3
+ # assigned as the model for @object_name@.
4
+ # @presenter_options@ are set once on the initial presenter instance.
5
+ #
6
+ def each_with_presenter(presenter_class, object_name, presenter_options={}, &block)
7
+ presenter = presenter_class.new(presenter_options)
8
+ each do |object|
9
+ presenter.send("#{object_name}=", object)
10
+ yield presenter
11
+ end
12
+ end
13
+ end
data/lib/viewtastic.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rails'
2
+ require "viewtastic/activation"
3
+ require "viewtastic/base"
4
+
5
+ module Viewtastic
6
+ VERSION = "0.3.0"
7
+
8
+ class Railtie < Rails::Railtie
9
+ initializer "viewtastic.activate" do
10
+ ActiveSupport.on_load(:action_controller) do
11
+ ActionController::Base.send(:include, Viewtastic::Activation)
12
+ end
13
+ end
14
+
15
+ initializer "viewtastic.add_presenter_paths" do
16
+ require "duck_punches/array/each_with_presenter"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module Viewtastic
2
+ module Activation
3
+ # Lets Viewtastic know about the controller object via a before filter, AKA "activates" viewtastic.
4
+ # Borrowed from Authlogic.
5
+ #
6
+ def self.included(klass) # :nodoc:
7
+ klass.prepend_before_filter :activate_viewtastic
8
+ end
9
+
10
+ private
11
+ def activate_viewtastic
12
+ Viewtastic::Base.controller = self
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,129 @@
1
+ module Viewtastic
2
+ # Base class for presenters. See README.md for usage.
3
+ #
4
+ class Base
5
+ include ActionView::Helpers::UrlHelper
6
+ include ActionView::Helpers::TextHelper
7
+ include ActionView::Helpers::TagHelper
8
+ include ActionView::Helpers::NumberHelper
9
+ include ActionView::Helpers::FormTagHelper
10
+
11
+ class_inheritable_accessor :presented
12
+ self.presented = []
13
+
14
+ delegate :protect_against_forgery?,
15
+ :request_forgery_protection_token,
16
+ :form_authenticity_token,
17
+ :dom_id,
18
+ :to => :controller
19
+
20
+ class << self
21
+ # Indicates which models are to be presented.
22
+ #
23
+ # class CommentPresenter < Viewtastic::Base
24
+ # presents :comment, :post
25
+ # end
26
+ #
27
+ # If you want to delegate messages to models without prefixing them with the model name, specify them in an Array:
28
+ #
29
+ # class PresenterWithTwoAddresses < ActivePresenter::Base
30
+ # presents :post, :comment => [:body, :created_at]
31
+ # end
32
+ #
33
+ def presents(*types)
34
+ types_and_attributes = types.extract_options!
35
+
36
+ types_and_attributes.each do |name, delegates|
37
+ attr_accessor name
38
+ presented << name
39
+ delegates.each do |msg|
40
+ delegate msg, :to => name
41
+ end
42
+ end
43
+
44
+ attr_accessor *types
45
+ self.presented += types
46
+
47
+ presented.each do |name|
48
+ define_method("#{name}_dom_id") do |*args|
49
+ controller.send(:dom_id, send(name), *args)
50
+ end
51
+ end
52
+ end
53
+
54
+ def controller=(value) #:nodoc:
55
+ Thread.current[:viewtastic_controller] = value
56
+ end
57
+
58
+ def controller #:nodoc:
59
+ Thread.current[:viewtastic_controller]
60
+ end
61
+
62
+ def activated? #:nodoc:
63
+ !controller.nil?
64
+ end
65
+ end
66
+
67
+ # Accepts arguments in two forms. If you had a CommentPresenter that presented a Comment model and a Post model, you would write the follwoing:
68
+ #
69
+ # 1. CommentPresenter.new(:comment => Comment.new, :post => @post)
70
+ # 2. CommentPresenter.new(Comment.new, @post) - it will introspect on the model's class; the order is not important.
71
+ #
72
+ # You can even mix the two:
73
+ # CommentPresenter.new(Comment.new, :post => @post)
74
+ #
75
+ def initialize(*values)
76
+ keys_and_values = values.extract_options!
77
+
78
+ keys_and_values.each do |name, instance|
79
+ send("#{name}=", instance)
80
+ end
81
+
82
+ values.each do |value|
83
+ send("#{value.class.name.underscore}=", value)
84
+ end
85
+ end
86
+
87
+ def method_missing(method_name, *args, &block)
88
+ if method_name.to_s =~ /_(path|url)$/
89
+ # Delegate all named routes to the controller
90
+ controller.send(method_name, *args)
91
+ elsif presented_attribute?(method_name)
92
+ delegate_message(method_name, *args, &block)
93
+ else
94
+ super
95
+ end
96
+ end
97
+
98
+ # The current controller performing the request is accessible with this.
99
+ #
100
+ def controller
101
+ self.class.controller
102
+ end
103
+
104
+ protected
105
+ def delegate_message(method_name, *args, &block) #:nodoc:
106
+ presentable = presentable_for(method_name)
107
+ send(presentable).send(flatten_attribute_name(method_name, presentable), *args, &block)
108
+ end
109
+
110
+ def presentable_for(method_name) #:nodoc:
111
+ presented.sort_by { |k| k.to_s.size }.reverse.detect do |type|
112
+ method_name.to_s.starts_with?(attribute_prefix(type))
113
+ end
114
+ end
115
+
116
+ def presented_attribute?(method_name) #:nodoc:
117
+ p = presentable_for(method_name)
118
+ !p.nil? && send(p).respond_to?(flatten_attribute_name(method_name,p))
119
+ end
120
+
121
+ def flatten_attribute_name(name, type) #:nodoc:
122
+ name.to_s.gsub(/^#{attribute_prefix(type)}/, '')
123
+ end
124
+
125
+ def attribute_prefix(type) #:nodoc:
126
+ "#{type}_"
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,15 @@
1
+ module Viewtastic
2
+ module TestCase
3
+ include Authlogic::TestCase
4
+
5
+ def activate_viewtastic
6
+ if @request && ! @request.respond_to?(:params)
7
+ class <<@request
8
+ alias_method :params, :parameters
9
+ end
10
+ end
11
+
12
+ Viewtastic::Base.controller = (@request && Authlogic::TestCase::RailsRequestAdapter.new(@request)) || controller
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: be9-viewtastic
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Istvan Hoka
14
+ - Oleg Dashevskii
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-01-15 00:00:00 +06:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description:
24
+ email: olegdashevskii@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - LICENSE
31
+ - README.md
32
+ files:
33
+ - History.txt
34
+ - LICENSE
35
+ - lib/duck_punches/array/each_with_presenter.rb
36
+ - lib/viewtastic.rb
37
+ - lib/viewtastic/activation.rb
38
+ - lib/viewtastic/base.rb
39
+ - lib/viewtastic/test_case.rb
40
+ - README.md
41
+ has_rdoc: true
42
+ homepage: http://github.com/be9/viewtastic
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Presenter implementation for Ruby on Rails 3.x
75
+ test_files: []
76
+