display_case 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,8 +32,6 @@ Your exhibits will look something like this:
32
32
  # app/exhibits/league_exhibit.rb
33
33
 
34
34
  class LeagueExhibit < DisplayCase::Exhibit
35
- # Note: the context parameter is new in the master branch, not yet released in the gem.
36
- # If you get an argument error, that's why
37
35
  def self.applicable_to?(object, context)
38
36
  object.class.name == 'League'
39
37
  end
@@ -78,6 +76,7 @@ Several configuration options can be set via an initializer:
78
76
  1. `definition_file_paths` Because Rails lazily-loads files, in development mode DisplayCase will search /app/exhibits to load the Exhibits found there. If your Exhibits are elsewhere, you can set `config.definition_file_paths = ['list/of/directories', 'to/find/exhibits']` in your initializers/display_case.rb.
79
77
  1. `explicit` By default this option is false and Exhibits will be dynamically added via the inherited callback.
80
78
  1. `exhibits` If `explicit` is true you must explicitly set the Exhibits you wish to use in the order you want them evaluated. You can set `config.exhibits = [AnExhibit,AnotherExhibit]` in your initializers/display_case.rb.
79
+ 1. `cache_store` If you configure a cache store, you can use it by calling the `cache` method in your Exhibits (see below).
81
80
 
82
81
  An example `initializers/display_case.rb`
83
82
  ```
@@ -85,9 +84,37 @@ DisplayCase.configure do |config|
85
84
  config.definition_file_paths = ['app/exhibits','some/other/path']
86
85
  config.explicit = true
87
86
  config.exhibits = [MyFirstExhibit,MySecondExhibit]
87
+ config.cache_store = Rails.configuration.action_controller.perform_caching ? Rails.cache : nil
88
88
  end
89
89
  ```
90
90
 
91
+ Caching
92
+ -------
93
+ You can cache the results of an operation in your exhibits by configuring DisplayCase to use a cache store, and then using the `cache` method.
94
+ If you do this, you ought not use a real cache in development mode, since you'll likely want to see changes you're making to code, which of
95
+ course won't happen if you cache the results.
96
+
97
+ Use the cache like you would in a Rails controller:
98
+
99
+ ```ruby
100
+ class LeagueExhibit < DisplayCase::Exhibit
101
+ def self.applicable_to?(object, context)
102
+ object.class.name == 'League'
103
+ end
104
+
105
+ def render(context)
106
+ cache key, options={} do
107
+ # something that takes a long while, which might make you want
108
+ # to cache this call to render
109
+ context.render(partial: 'leagues/icon', locals: {league: self})
110
+ end
111
+ end
112
+ end
113
+ ```
114
+
115
+ See [How key-based cache expiration works](http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works) for examples on
116
+ how to choose good keys.
117
+
91
118
  Wrong url with extra parameters using an exhibited model?
92
119
  ------------------
93
120
  See this issue for the reason: https://github.com/objects-on-rails/display-case/issues/8
data/lib/display_case.rb CHANGED
@@ -4,4 +4,3 @@ require_relative 'display_case/enumerable_exhibit'
4
4
  require_relative 'display_case/basic_exhibit'
5
5
  require_relative 'display_case/exhibits_helper'
6
6
  require_relative 'display_case/find_definitions'
7
-
@@ -21,10 +21,14 @@ module DisplayCase
21
21
  # existing file will be loaded.
22
22
  attr_accessor :definition_file_paths
23
23
 
24
+ # A cache store which responds to `fetch(key, options, &block)`
25
+ attr_accessor :cache_store
26
+
24
27
  def initialize
25
28
  @definition_file_paths = %w(app/exhibits)
26
29
  @explicit = false
27
30
  @exhibits = []
31
+ @cache_store = nil
28
32
  end
29
33
 
30
34
  def explicit?
@@ -45,6 +45,11 @@ module DisplayCase
45
45
  def to_ary
46
46
  self
47
47
  end
48
+
49
+ # See https://github.com/objects-on-rails/display-case/issues/27
50
+ def to_json
51
+ as_json.to_json
52
+ end
48
53
 
49
54
  def render(template)
50
55
  inject(ActiveSupport::SafeBuffer.new) { |output,element|
@@ -29,9 +29,10 @@ module DisplayCase
29
29
  ::Rails.logger.debug "Exhibit context: #{context}"
30
30
  end
31
31
 
32
+ similar, unsimilar = exhibits.partition { |exhibit_class| context and exhibit_class.name and context.class.name and context.class.name.downcase.include?(exhibit_class.name.to_s.downcase.gsub("exhibit", "")) }
32
33
  object = BasicExhibit.new(Exhibited.new(object, context), context)
33
- exhibits.inject(object) do |object, exhibit_class|
34
- exhibit_class.exhibit_if_applicable(object, context)
34
+ (unsimilar+similar).inject(object) do |object, exhibit_class|
35
+ exhibit_class.exhibit_if_applicable(object, context)
35
36
  end.tap do |obj|
36
37
  ::Rails.logger.debug "Exhibits applied: #{obj.inspect_exhibits}" if defined? ::Rails
37
38
  end
@@ -128,6 +129,14 @@ module DisplayCase
128
129
  def exhibited?
129
130
  true
130
131
  end
132
+
133
+ def cache(key, options = {}, &block)
134
+ if DisplayCase.configuration.cache_store
135
+ DisplayCase.configuration.cache_store.fetch(key, options, &block)
136
+ else
137
+ yield
138
+ end
139
+ end
131
140
 
132
141
  private
133
142
 
@@ -3,7 +3,7 @@ module DisplayCase
3
3
  # http://guides.rubyonrails.org/configuring.html#initialization-events
4
4
  # "to_prepare will run upon every request in development, but only once (during boot-up) in production and test."
5
5
  config.to_prepare do
6
- DisplayCase.find_definitions
7
- end
6
+ DisplayCase.find_definitions if Rails.env.development?
7
+ end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: display_case
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: 2013-02-14 00:00:00.000000000Z
12
+ date: 2013-05-29 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: An implementation of the Exhibit pattern, as described in Objects on
15
15
  Rails