condi 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +1 -1
  2. data/lib/condi.rb +15 -1
  3. data/test/test_condi.rb +19 -1
  4. metadata +6 -3
data/README.md CHANGED
@@ -112,7 +112,7 @@ returns the css class we need for a given item?
112
112
 
113
113
  `app/views/store/_item.html.erb:`
114
114
 
115
- <tr class="<%= css_for_item_status %>">
115
+ <tr class="<%= css_for_item_status(item) %>">
116
116
  <td><%= item.to_s %></td>
117
117
  </tr>
118
118
 
@@ -30,10 +30,24 @@ module Condi
30
30
  # @param [Proc] block {} or do...end block.
31
31
  # @note A *synonym* is an alias for defining methods that return values other than true or false.
32
32
  # @note You are not required to end a predicate with a question mark, however it is conventional in Ruby to do so.
33
+ # @note Predicates can only be called during the request context they are defined in, otherwise a RuntimeError is raised. This restriction prevents the associated closures from inadvertently leaking previous request data when the controller classes are cached (i.e. in production).
33
34
  # @see the full example in the <a href="index.html">README</a>.
34
35
  def predicate(method_name, &block)
35
36
  self.class.instance_eval do
36
- define_method(method_name, &block)
37
+ # this is the request id at the moment the predicate is defined
38
+ request_id = eval("request.object_id",block.binding)
39
+
40
+ define_method(method_name) do |*args|
41
+ # this is the request id at the moment the predicate is called
42
+ check_request_id = request.object_id
43
+
44
+ # if they don't match, raise an error!
45
+ unless check_request_id == request_id
46
+ #debugger
47
+ raise RuntimeError, "predicate '#{method_name}' cannot be called outside of the request it was defined in (#{request_id}). please redefine the predicate in this request (#{check_request_id}).", caller(2)
48
+ end
49
+ block.call(*args)
50
+ end
37
51
  helper_method(method_name)
38
52
  end
39
53
  end
@@ -9,7 +9,7 @@ require 'condi'
9
9
 
10
10
  require 'ostruct'
11
11
 
12
- #require 'ruby-debug'
12
+ #require 'debugger'
13
13
 
14
14
 
15
15
  class CondiTest < Test::Unit::TestCase
@@ -142,4 +142,22 @@ class CondiTest < Test::Unit::TestCase
142
142
  end
143
143
 
144
144
 
145
+ # fix for https://github.com/coldnebo/condi/issues/1
146
+ def test_lifespan
147
+ @controller_instance.request = Object.new # simulate the first request
148
+ @controller_instance.instance_eval do
149
+ predicate(:always_be_true?) { true }
150
+ end
151
+
152
+ assert @controller_instance.respond_to?(:always_be_true?)
153
+ assert @controller_instance.always_be_true? == true
154
+
155
+ @controller_instance.request = Object.new # simulate a second request
156
+
157
+ # now, if a view calls the predicate, we shouldn't allow it!
158
+ assert_raise RuntimeError do
159
+ @controller_instance.always_be_true?
160
+ end
161
+ end
162
+
145
163
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: condi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-14 00:00:00.000000000 Z
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Conditional UI predicates for Rails - a clean and simple approach to
15
15
  separate business logic from your views and models.
@@ -34,6 +34,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
34
34
  - - ! '>='
35
35
  - !ruby/object:Gem::Version
36
36
  version: '0'
37
+ segments:
38
+ - 0
39
+ hash: -2574211362377765017
37
40
  required_rubygems_version: !ruby/object:Gem::Requirement
38
41
  none: false
39
42
  requirements:
@@ -43,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
46
  requirements:
44
47
  - rails
45
48
  rubyforge_project:
46
- rubygems_version: 1.8.10
49
+ rubygems_version: 1.8.24
47
50
  signing_key:
48
51
  specification_version: 3
49
52
  summary: Condi