rooftop 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17838c2957390cba459e722b6b6c24e107db27c2
4
- data.tar.gz: 7a2616214f7e5e5ac829e34c53d4d191a7fef6b6
3
+ metadata.gz: f7b51337bae47ac92f6fb41b5716bbe292f3f19b
4
+ data.tar.gz: c0ee1e20299e906b1fcf0358e820ddf05ae3d31a
5
5
  SHA512:
6
- metadata.gz: 7e043daa4269c79ed27af44fc7e18b00d07f11227b88404807c018608b791510a1618fdc4f367eca7e0f98903b5922f4ac550c09d55ecaadf7b840476496059a
7
- data.tar.gz: e2c2142efc855d53db5e9443bcaf9ec3877b86184cec3a3f7c013b7b379cbc8ca34083739685b4e2279971f84acb58a38d897fb0a409eee8d0e1f9656a146782
6
+ metadata.gz: 440657fbf5aa59c58464a661e57f04c7459f78b4efe4e7796c0180ed2c6e0064f83097e17cccbb397d5de151636ba128c1f45b06e2c8a443092faf3d3c65d62f
7
+ data.tar.gz: 2c0e502abd0d09977f803d6354a22b4ae3b762044513d7793847d46d225879e6a1ceb57136b98bbb288fb71fa8629e31bd40d1112bb29b41ac28602126afbf37
data/README.md CHANGED
@@ -82,7 +82,7 @@ Sometimes you want to do something with a field after it's returned. for example
82
82
  To coerce one or more fields, call a class method in your class and pass a lambda which is called on the field.
83
83
 
84
84
  ```
85
- class mycustomposttype
85
+ class MyCustomPostType
86
86
  include rooftop::post
87
87
  self.post_type = "my_custom_post_type"
88
88
  coerce_field date: ->(date) { datetime.parse(date)}
@@ -191,7 +191,36 @@ _(the cache headers plugin for rooftop is a work in progress)_
191
191
  If you want to cache responses, set `perform_caching` to true in your configuration block. you will need to provide a cache store and logger in the `cache_store` and `cache_logger` config options. by default, the cache store is set to `activesupport::cache.lookup_store(:memory_store)` which is a sensible default, but isn't shared across threads. any activesupport-compatible cache store (memcache, file, redis etc.) will do.
192
192
 
193
193
  The `cache_logger` option determines where cache debug messages (hits, misses etc.) get stored. by default it's `nil`, which switches logging off.
194
-
194
+
195
+ ## Menus
196
+ WordPress comes with quite a powerful way to build menus, rather than needing to traverse a tree of objects directly.
197
+
198
+ ### Accessing the menu you need
199
+ At the moment you have to know the ID of the menu you need - not ideal. Keep an eye on the following issues to get an update:
200
+
201
+ * https://github.com/rooftopcms/rooftop-custom-content-setup/issues/3
202
+ * https://github.com/rooftopcms/rooftop-custom-content-setup/issues/4
203
+
204
+ So for now, with that in mind, here's how you do it:
205
+
206
+ ```
207
+ r = Rooftop::Menus::Menu.find(2) #returns a menu with ID 2
208
+ r.items # a collection of Rooftop::Menus::Item
209
+ ```
210
+
211
+ ### Accessing the object to which a menu item refers
212
+ If a menu item refers to an object in Rooftop - a post or a page - you can access the original object by calling `object()`.
213
+
214
+ ```
215
+ r = Rooftop::Menus::Menu.find(2) #returns a menu with ID 2
216
+ r.items # a collection of Rooftop::Menus::Item
217
+ item = r.items.first # a Rooftop::Menus::Item
218
+ item.object # an object derived from the post type and ID.
219
+ ```
220
+
221
+ If you haven't defined a class for a post type, you'll get a `Rooftop::Menus::UnmappedObjectError`
222
+
223
+
195
224
  # Roadmap
196
225
  Lots! here's a flavour:
197
226
 
@@ -199,7 +228,6 @@ Lots! here's a flavour:
199
228
 
200
229
  * preview mode. rooftop supports passing a preview header to see content in draft. we'll expose that in the rooftop gem as a constant.
201
230
  * taxonomies will be supported and side-loaded against content
202
- * menus are exposed by rooftop. we need to create a rooftop::menu mixin
203
231
  * media: media is exposed by the api, and should be accessible and downloadable.
204
232
 
205
233
  ## Writing
data/lib/rooftop.rb CHANGED
@@ -25,7 +25,7 @@ module Rooftop
25
25
  end
26
26
 
27
27
  class Configuration
28
- attr_accessor :api_token, :url, :site_name, :perform_caching, :cache_store, :cache_logger, :ssl_options, :proxy
28
+ attr_accessor :api_token, :url, :site_name, :perform_caching, :cache_store, :cache_logger, :ssl_options, :proxy, :post_type_mapping
29
29
  attr_reader :connection,
30
30
  :connection_path,
31
31
  :api_path, #actually writeable with custom setter
@@ -44,6 +44,7 @@ module Rooftop
44
44
  @cache_logger = nil
45
45
  @ssl_options = {}
46
46
  @proxy = nil
47
+ @post_type_mapping = {}
47
48
  end
48
49
 
49
50
  def api_path=(path)
data/lib/rooftop/base.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module Rooftop
2
2
  module Base
3
3
  def self.included(base)
4
+ @included_classes ||= []
5
+ @included_classes << base unless @included_classes.include?(base)
4
6
  base.extend ClassMethods
5
7
  base.include Her::Model
6
8
 
@@ -37,6 +39,10 @@ module Rooftop
37
39
 
38
40
  end
39
41
 
42
+ def self.included_classes
43
+ @included_classes
44
+ end
45
+
40
46
  module ClassMethods
41
47
  attr_reader :api_namespace, :api_version, :api_endpoint
42
48
 
@@ -58,6 +64,7 @@ module Rooftop
58
64
  def setup_path!
59
65
  @api_endpoint ||= collection_path
60
66
  self.collection_path "#{@api_namespace}/v#{@api_version}/#{@api_endpoint}"
67
+
61
68
  end
62
69
 
63
70
  # Allow calling 'first'
@@ -0,0 +1,5 @@
1
+ module Rooftop
2
+ module Menus
3
+ class UnmappedObjectError < NoMethodError; end
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ module Rooftop
2
+ module Menus
3
+ class Item < OpenStruct
4
+ def initialize(args)
5
+ args[:object_type] = args[:object]
6
+ super
7
+ # If this menu item has children, create a MenuItem for each one
8
+ if self.children
9
+ self.children = children.collect do |child|
10
+ Item.new(child)
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ def object
17
+ if self.type == "post_type"
18
+ begin
19
+ klass = Rooftop.configuration.post_type_mapping[self.object_type] || self.object_type.classify.constantize
20
+ klass.find_by(slug: self.slug).first
21
+ rescue
22
+ raise UnmappedObjectError, "Couldn't find an mapping between the #{self.object_type} post type and a class in your code."
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ module Rooftop
2
+ module Menus
3
+ class Menu
4
+ include Rooftop::Base
5
+ self.api_namespace = "wp-api-menus"
6
+ self.api_version = 2
7
+ coerce_field items: ->(items) { items.collect {|i| Item.new(i)} unless items.nil?}
8
+ end
9
+ end
10
+ end
data/lib/rooftop/post.rb CHANGED
@@ -8,6 +8,7 @@ module Rooftop
8
8
 
9
9
  module ClassMethods
10
10
  def post_type=(type)
11
+ Rooftop.configuration.post_type_mapping.merge!(type => self)
11
12
  self.api_endpoint = type.pluralize
12
13
  end
13
14
  end
@@ -12,9 +12,28 @@ module Rooftop
12
12
  # the fact that 'slug' is referred to in the db as 'name' is irritating. Let's fix that
13
13
  # in queries so we can specify {slug: "foo"}
14
14
  if args.keys.collect(&:to_sym).include?(:slug)
15
- args[:name] = args[:slug]
15
+ if args[:slug].is_a?(Array)
16
+ args[:post_name__in] ||= []
17
+ args[:slug].each do |slug|
18
+ args[:post_name__in] << slug
19
+ end
20
+ else
21
+ args[:name] = args[:slug]
22
+ end
16
23
  args.delete(:slug)
17
24
  end
25
+
26
+ if args.keys.collect(&:to_sym).include?(:id)
27
+ if args[:id].is_a?(Array)
28
+ args[:post__in] ||= []
29
+ args[:id].each do |id|
30
+ args[:post__in] << id
31
+ end
32
+ else
33
+ args[:page_id] = args[:id]
34
+ end
35
+ args.delete(:id)
36
+ end
18
37
  filters = args.inject({}) {|hash,pair| hash["filter[#{pair.first}]"] = pair.last; hash}
19
38
  #Call the Her `where` method with our new filters
20
39
  super().where(filters)
@@ -1,3 +1,3 @@
1
1
  module Rooftop
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rooftop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ed Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-09 00:00:00.000000000 Z
11
+ date: 2015-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,13 +117,14 @@ files:
117
117
  - lib/rooftop/content/field.rb
118
118
  - lib/rooftop/errors/field_not_found_error.rb
119
119
  - lib/rooftop/errors/record_not_found_error.rb
120
+ - lib/rooftop/errors/unmapped_object_error.rb
120
121
  - lib/rooftop/field_aliases.rb
121
122
  - lib/rooftop/headers.rb
122
123
  - lib/rooftop/hook_calls.rb
124
+ - lib/rooftop/menus/item.rb
125
+ - lib/rooftop/menus/menu.rb
123
126
  - lib/rooftop/models/author.rb
124
127
  - lib/rooftop/models/media_item.rb
125
- - lib/rooftop/models/menu.rb
126
- - lib/rooftop/models/menu_item.rb
127
128
  - lib/rooftop/models/taxonomy.rb
128
129
  - lib/rooftop/models/taxonomy_term.rb
129
130
  - lib/rooftop/nested.rb
@@ -1,9 +0,0 @@
1
- module Rooftop
2
- class Menu
3
- include Rooftop::Base
4
- has_many :menu_items, class: "Rooftop::MenuItem"
5
- self.api_namespace = "wp-api-menus"
6
- self.api_version = 2
7
- # coerce_field items: ->(items) { items.collect {|i| MenuItem.new(i)} unless items.nil?}
8
- end
9
- end
@@ -1,18 +0,0 @@
1
- module Rooftop
2
- class MenuItem
3
- def initialize(args)
4
- args.each do |k,v|
5
- instance_variable_set("@#{k}", v)
6
- self.class.send(:attr_accessor, k)
7
- end
8
- end
9
-
10
- def id
11
- self.instance_variable_get(:"@ID")
12
- end
13
-
14
- def ==(other)
15
- other.class == self.class && other.respond_to?(:id) && other.id == id
16
- end
17
- end
18
- end