viewtastic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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 ADDED
@@ -0,0 +1,13 @@
1
+ Viewtastic
2
+ ==========
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2009 [name of plugin creator], released under the MIT license
@@ -0,0 +1,9 @@
1
+ class Array
2
+ def each_with_presenter(presenter_class, object_name, presenter_options={}, &block)
3
+ presenter = presenter_class.new(presenter_options)
4
+ each do |object|
5
+ presenter.send("#{object_name}=", object)
6
+ yield presenter
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module Viewtastic
2
+ module Activation
3
+ # Lets Viewtastic know about the controller object via a before filter, AKA "activates" viewtastic.
4
+ # Borrowed from Viewtastic.
5
+ #
6
+ def self.included(klass) # :nodoc:
7
+ if defined?(::ApplicationController)
8
+ raise "Viewtastic is trying to prepend a before_filter in ActionController::Base to active itself" +
9
+ ", the problem is that ApplicationController has already been loaded meaning the before_filter won't get copied into your" +
10
+ " application. Generally this is due to another gem or plugin requiring your ApplicationController prematurely, such as" +
11
+ " the resource_controller plugin. The solution is to require Viewtastic before these other gems / plugins. Please require" +
12
+ " viewtastic first to get rid of this error."
13
+ end
14
+
15
+ klass.prepend_before_filter :activate_viewtastic
16
+ end
17
+
18
+ private
19
+ def activate_viewtastic
20
+ Viewtastic::Base.controller = self
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,101 @@
1
+ class Viewtastic::Base
2
+ include ActionView::Helpers::UrlHelper
3
+ include ActionView::Helpers::TextHelper
4
+ include ActionView::Helpers::TagHelper
5
+
6
+ class_inheritable_accessor :presented
7
+ self.presented = []
8
+
9
+ delegate :protect_against_forgery?,
10
+ :request_forgery_protection_token,
11
+ :form_authenticity_token,
12
+ :dom_id,
13
+ :to => :controller
14
+
15
+ class << self
16
+ def presents(*types)
17
+ types_and_attributes = types.extract_options!
18
+
19
+ types_and_attributes.each do |name, delegates|
20
+ attr_accessor name
21
+ presented << name
22
+ delegates.each do |msg|
23
+ delegate msg, :to => name
24
+ end
25
+ end
26
+
27
+ attr_accessor *types
28
+ self.presented += types
29
+
30
+ presented.each do |name|
31
+ define_method("#{name}_dom_id") do |*args|
32
+ send(:dom_id, send(name), *args)
33
+ end
34
+ end
35
+ end
36
+
37
+ def controller=(value)
38
+ Thread.current[:viewtastic_controller] = value
39
+ end
40
+
41
+ def controller
42
+ Thread.current[:viewtastic_controller]
43
+ end
44
+
45
+ def activated?
46
+ !controller.nil?
47
+ end
48
+ end
49
+
50
+ def initialize(*values)
51
+ keys_and_values = values.extract_options!
52
+
53
+ keys_and_values.each do |name, instance|
54
+ send("#{name}=", instance)
55
+ end
56
+
57
+ values.each do |value|
58
+ send("#{value.class.name.underscore}=", value)
59
+ end
60
+ end
61
+
62
+ def method_missing(method_name, *args, &block)
63
+ if method_name.to_s =~ /_(path|url)$/
64
+ # Delegate all named routes to the controller
65
+ controller.send(method_name, *args)
66
+ elsif presented_attribute?(method_name)
67
+ delegate_message(method_name, *args, &block)
68
+ else
69
+ super
70
+ end
71
+ end
72
+
73
+ def controller
74
+ self.class.controller
75
+ end
76
+
77
+ protected
78
+ def delegate_message(method_name, *args, &block)
79
+ presentable = presentable_for(method_name)
80
+ send(presentable).send(flatten_attribute_name(method_name, presentable), *args, &block)
81
+ end
82
+
83
+ def presentable_for(method_name)
84
+ presented.sort_by { |k| k.to_s.size }.reverse.detect do |type|
85
+ method_name.to_s.starts_with?(attribute_prefix(type))
86
+ end
87
+ end
88
+
89
+ def presented_attribute?(method_name)
90
+ p = presentable_for(method_name)
91
+ !p.nil? && send(p).respond_to?(flatten_attribute_name(method_name,p))
92
+ end
93
+
94
+ def flatten_attribute_name(name, type)
95
+ name.to_s.gsub(/^#{attribute_prefix(type)}/, '')
96
+ end
97
+
98
+ def attribute_prefix(type)
99
+ "#{type}_"
100
+ end
101
+ 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
data/lib/viewtastic.rb ADDED
@@ -0,0 +1,3 @@
1
+ require File.dirname(__FILE__) + "/viewtastic/activation"
2
+ require File.dirname(__FILE__) + "/viewtastic/base"
3
+ require File.dirname(__FILE__) + "/duck_punches/array/each_with_presenter"
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: viewtastic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Istvan Hoka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-30 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: istvan.hoka@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README
25
+ files:
26
+ - LICENSE
27
+ - lib/duck_punches/array/each_with_presenter.rb
28
+ - lib/viewtastic.rb
29
+ - lib/viewtastic/activation.rb
30
+ - lib/viewtastic/base.rb
31
+ - lib/viewtastic/test_case.rb
32
+ - README
33
+ has_rdoc: true
34
+ homepage: http://github.com/ihoka/viewtastic
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Presenter plugin for Ruby on Rails
61
+ test_files: []
62
+