Class: Kiosk::WordPress::Resource
- Inherits:
-
ActiveResource::Base
- Object
- ActiveResource::Base
- Kiosk::WordPress::Resource
- Includes:
- Cacheable::Resource, Localizable::Resource, Prospector
- Defined in:
- lib/kiosk/word_press/resource.rb
Class Method Summary collapse
-
.all ⇒ Object
Returns all instances of the resource.
-
.collection_path(prefix_options = {}, query_options = nil) ⇒ Object
Reimplements the
ActiveResource
path constructor to work with the WordPress JSON-API plugin. -
.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object
Reimplements the
ActiveResource
path constructor to work with the WordPress JSON-API plugin. - .element_path_by_slug(slug, prefix_options = {}, query_options = nil) ⇒ Object
-
.find(*arguments) ⇒ Object
Adds functionality to the
ActiveResource.find
method to allow for specifying the WordPress JSON API method that should be used. -
.find_by_associated(resource, params = {}) ⇒ Object
Finds all resources by the given related resource.
-
.find_by_slug(slug) ⇒ Object
Finds the resource by the given slug.
-
.instantiate_collection(collection, prefix_options = {}) ⇒ Object
Reimplements the
ActiveResource
method to check for bad responses before instantiating a collection. -
.instantiate_record(record, prefix_options = {}) ⇒ Object
Reimplements the
ActiveResource
method to check for bad responses before instantiating an object. -
.with_parameters(params = {}) ⇒ Object
Executes the given block within a scope where all requests for this content resource are appended with the given parameters.
Instance Method Summary collapse
-
#content ⇒ Object
Returns the rewritten resource content.
-
#content_document ⇒ Object
Returns the rewritten resource content as a
Document
. -
#destroy ⇒ Object
Destroying is not supported.
-
#excerpt ⇒ Object
Returns the rewritten resource excerpt.
-
#raw_content ⇒ Object
Returns the resource content, untouched by the content rewriter.
-
#raw_excerpt ⇒ Object
Returns the resource excerpt, untouched by the content rewriter.
-
#save ⇒ Object
Saving is not supported.
-
#to_param ⇒ Object
Returns the value used in constructing a URL to this object.
Methods included from Cacheable::Resource
Class Method Details
.all ⇒ Object
Returns all instances of the resource.
33 34 35 |
# File 'lib/kiosk/word_press/resource.rb', line 33 def all find(:all) end |
.collection_path(prefix_options = {}, query_options = nil) ⇒ Object
Reimplements the ActiveResource
path constructor to work with the WordPress JSON-API plugin.
89 90 91 |
# File 'lib/kiosk/word_press/resource.rb', line 89 def collection_path( = {}, = nil) "#{api_path_to("get_#{element_name}_index")}#{query_string()}" end |
.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object
Reimplements the ActiveResource
path constructor to work with the WordPress JSON-API plugin.
40 41 42 |
# File 'lib/kiosk/word_press/resource.rb', line 40 def element_path(id, = {}, = nil) "#{api_path_to("get_#{element_name}")}#{query_string({:id => id}.merge( || {}))}" end |
.element_path_by_slug(slug, prefix_options = {}, query_options = nil) ⇒ Object
44 45 46 |
# File 'lib/kiosk/word_press/resource.rb', line 44 def element_path_by_slug(slug, = {}, = nil) "#{api_path_to("get_#{element_name}")}#{query_string({:slug => slug}.merge( || {}))}" end |
.find(*arguments) ⇒ Object
Adds functionality to the ActiveResource.find
method to allow for specifying the WordPress JSON API method that should be used. This simplifies definition of scopes in derived models.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kiosk/word_press/resource.rb', line 52 def find(*arguments) scope = arguments.slice!(0) = arguments.slice!(0) || {} if .key?(:method) [:from] = api_path_to([:method]) .delete(:method) end super(scope, ) end |
.find_by_associated(resource, params = {}) ⇒ Object
74 75 76 77 78 |
# File 'lib/kiosk/word_press/resource.rb', line 74 def find_by_associated(resource, params = {}) find(:all, :method => "get_#{resource.class.element_name}_#{element_name.pluralize}", :params => params.merge({:id => resource.id})) end |
.find_by_slug(slug) ⇒ Object
Finds the resource by the given slug.
82 83 84 |
# File 'lib/kiosk/word_press/resource.rb', line 82 def find_by_slug(slug) find(:one, :method => "get_#{element_name}", :params => {:slug => slug}) end |
.instantiate_collection(collection, prefix_options = {}) ⇒ Object
Reimplements the ActiveResource
method to check for bad responses before instantiating a collection.
96 97 98 |
# File 'lib/kiosk/word_press/resource.rb', line 96 def instantiate_collection(collection, = {}) super(normalize_response(collection, true), ) end |
.instantiate_record(record, prefix_options = {}) ⇒ Object
Reimplements the ActiveResource
method to check for bad responses before instantiating an object.
103 104 105 |
# File 'lib/kiosk/word_press/resource.rb', line 103 def instantiate_record(record, = {}) super(normalize_response(record), ) end |
.with_parameters(params = {}) ⇒ Object
Executes the given block within a scope where all requests for this content resource are appended with the given parameters.
class Post < Resource; end
Post.with_parameters(:language => 'en') do
english_posts = Post.find(:all)
english_pages = Page.find(:all)
end
Scopes can be nested.
Post.with_parameters(:language => 'es') do
Post.with_parameters(:recent => true) do
recent_spanish_posts = Post.find(:all)
end
end
Scopes are inherited.
Resource.with_parameters(:language => 'fr') do
french_posts = Post.find(:all)
end
However, nesting is still respected.
Resource.with_parameters(:language => 'fr') do
Post.with_parameters(:language => 'en') do
english_posts = Post.find(:all)
end
end
Even with this nesting inverted.
Post.with_parameters(:language => 'fr') do
Resource.with_parameters(:language => 'en') do
english_posts = Post.find(:all)
end
end
147 148 149 150 151 152 153 154 155 |
# File 'lib/kiosk/word_press/resource.rb', line 147 def with_parameters(params = {}) push_to_query_scope_stack(params) begin yield ensure pop_from_query_scope_stack end end |
Instance Method Details
#content ⇒ Object
Returns the rewritten resource content. See raw_content
for untouched content.
273 274 275 |
# File 'lib/kiosk/word_press/resource.rb', line 273 def content @content ||= raw_content && Kiosk.rewriter.rewrite(raw_content) end |
#content_document ⇒ Object
Returns the rewritten resource content as a Document
.
279 280 281 |
# File 'lib/kiosk/word_press/resource.rb', line 279 def content_document raw_content && Kiosk.rewriter.rewrite_to_document(raw_content) end |
#destroy ⇒ Object
Destroying is not supported.
292 293 294 |
# File 'lib/kiosk/word_press/resource.rb', line 292 def destroy raise NotImplementedError end |
#excerpt ⇒ Object
Returns the rewritten resource excerpt. See raw_excerpt
for untouched content.
286 287 288 |
# File 'lib/kiosk/word_press/resource.rb', line 286 def excerpt @excerpt ||= raw_excerpt && Kiosk.rewriter.rewrite(raw_excerpt) end |
#raw_content ⇒ Object
Returns the resource content, untouched by the content rewriter.
298 299 300 |
# File 'lib/kiosk/word_press/resource.rb', line 298 def raw_content attributes[:content] end |
#raw_excerpt ⇒ Object
Returns the resource excerpt, untouched by the content rewriter.
304 305 306 |
# File 'lib/kiosk/word_press/resource.rb', line 304 def raw_excerpt attributes[:excerpt] end |
#save ⇒ Object
Saving is not supported.
310 311 312 |
# File 'lib/kiosk/word_press/resource.rb', line 310 def save raise NotImplementedError end |
#to_param ⇒ Object
Returns the value used in constructing a URL to this object.
316 317 318 |
# File 'lib/kiosk/word_press/resource.rb', line 316 def to_param attributes[:slug] || attributes[:id] end |