be9-viewtastic 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/viewtastic/base.rb +29 -24
  2. metadata +8 -8
@@ -8,23 +8,28 @@ module Viewtastic
8
8
  include ActionView::Helpers::NumberHelper
9
9
  include ActionView::Helpers::FormTagHelper
10
10
 
11
- class_inheritable_accessor :presented
11
+ if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR == 0
12
+ class_inheritable_accessor :presented
13
+ else
14
+ class_attribute :presented
15
+ end
16
+
12
17
  self.presented = []
13
-
18
+
14
19
  delegate :protect_against_forgery?,
15
20
  :request_forgery_protection_token,
16
21
  :form_authenticity_token,
17
22
  :dom_id,
18
23
  :to => :controller
19
-
24
+
20
25
  class << self
21
26
  # Indicates which models are to be presented.
22
27
  #
23
28
  # class CommentPresenter < Viewtastic::Base
24
29
  # presents :comment, :post
25
30
  # end
26
- #
27
- # If you want to delegate messages to models without prefixing them with the model name, specify them in an Array:
31
+ #
32
+ # If you want to delegate messages to models without prefixing them with the model name, specify them in an Array:
28
33
  #
29
34
  # class PresenterWithTwoAddresses < ActivePresenter::Base
30
35
  # presents :post, :comment => [:body, :created_at]
@@ -32,7 +37,7 @@ module Viewtastic
32
37
  #
33
38
  def presents(*types)
34
39
  types_and_attributes = types.extract_options!
35
-
40
+
36
41
  types_and_attributes.each do |name, delegates|
37
42
  attr_accessor name
38
43
  presented << name
@@ -40,51 +45,51 @@ module Viewtastic
40
45
  delegate msg, :to => name
41
46
  end
42
47
  end
43
-
48
+
44
49
  attr_accessor *types
45
50
  self.presented += types
46
-
51
+
47
52
  presented.each do |name|
48
53
  define_method("#{name}_dom_id") do |*args|
49
54
  controller.send(:dom_id, send(name), *args)
50
55
  end
51
56
  end
52
57
  end
53
-
58
+
54
59
  def controller=(value) #:nodoc:
55
60
  Thread.current[:viewtastic_controller] = value
56
61
  end
57
-
62
+
58
63
  def controller #:nodoc:
59
64
  Thread.current[:viewtastic_controller]
60
65
  end
61
-
66
+
62
67
  def activated? #:nodoc:
63
68
  !controller.nil?
64
69
  end
65
70
  end
66
-
71
+
67
72
  # 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
- #
73
+ #
69
74
  # 1. CommentPresenter.new(:comment => Comment.new, :post => @post)
70
75
  # 2. CommentPresenter.new(Comment.new, @post) - it will introspect on the model's class; the order is not important.
71
- #
76
+ #
72
77
  # You can even mix the two:
73
78
  # CommentPresenter.new(Comment.new, :post => @post)
74
79
  #
75
80
  def initialize(*values)
76
81
  keys_and_values = values.extract_options!
77
-
82
+
78
83
  keys_and_values.each do |name, instance|
79
84
  send("#{name}=", instance)
80
85
  end
81
-
86
+
82
87
  values.each do |value|
83
88
  send("#{value.class.name.underscore}=", value)
84
89
  end
85
90
  end
86
-
87
- def method_missing(method_name, *args, &block)
91
+
92
+ def method_missing(method_name, *args, &block)
88
93
  if method_name.to_s =~ /_(path|url)$/
89
94
  # Delegate all named routes to the controller
90
95
  controller.send(method_name, *args)
@@ -94,34 +99,34 @@ module Viewtastic
94
99
  super
95
100
  end
96
101
  end
97
-
102
+
98
103
  # The current controller performing the request is accessible with this.
99
104
  #
100
105
  def controller
101
106
  self.class.controller
102
107
  end
103
-
108
+
104
109
  protected
105
110
  def delegate_message(method_name, *args, &block) #:nodoc:
106
111
  presentable = presentable_for(method_name)
107
112
  send(presentable).send(flatten_attribute_name(method_name, presentable), *args, &block)
108
113
  end
109
-
114
+
110
115
  def presentable_for(method_name) #:nodoc:
111
116
  presented.sort_by { |k| k.to_s.size }.reverse.detect do |type|
112
117
  method_name.to_s.starts_with?(attribute_prefix(type))
113
118
  end
114
119
  end
115
-
120
+
116
121
  def presented_attribute?(method_name) #:nodoc:
117
122
  p = presentable_for(method_name)
118
123
  !p.nil? && send(p).respond_to?(flatten_attribute_name(method_name,p))
119
124
  end
120
-
125
+
121
126
  def flatten_attribute_name(name, type) #:nodoc:
122
127
  name.to_s.gsub(/^#{attribute_prefix(type)}/, '')
123
128
  end
124
-
129
+
125
130
  def attribute_prefix(type) #:nodoc:
126
131
  "#{type}_"
127
132
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: be9-viewtastic
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
4
+ hash: 17
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Istvan Hoka
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-01-15 00:00:00 +06:00
19
+ date: 2011-05-24 00:00:00 +07:00
20
20
  default_executable:
21
21
  dependencies: []
22
22
 
@@ -43,8 +43,8 @@ homepage: http://github.com/be9/viewtastic
43
43
  licenses: []
44
44
 
45
45
  post_install_message:
46
- rdoc_options:
47
- - --charset=UTF-8
46
+ rdoc_options: []
47
+
48
48
  require_paths:
49
49
  - lib
50
50
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements: []
69
69
 
70
70
  rubyforge_project:
71
- rubygems_version: 1.3.7
71
+ rubygems_version: 1.6.2
72
72
  signing_key:
73
73
  specification_version: 3
74
74
  summary: Presenter implementation for Ruby on Rails 3.x