Module: Rooftop::Content
- Defined in:
- lib/rooftop/content/content_fields.rb,
lib/rooftop/content/field.rb,
lib/rooftop/content/collection.rb,
lib/rooftop/errors/preview_key_mismatch.rb,
lib/rooftop/errors/field_not_found_error.rb
Overview
The Rooftop API returns content for basic and advanced custom fields together. This module cleans up the response, and creates a collection of ContentField objects, with which we can do things like parse links.
Defined Under Namespace
Classes: Collection, Field, FieldNotFoundError, PreviewKeyMismatchError, PreviewKeyMissingError
Class Method Summary collapse
Instance Method Summary collapse
-
#has_field?(name, comparison = nil) ⇒ Boolean
test whether an instance has a field by name.
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rooftop/content/content_fields.rb', line 6 def self.included(base) base.include Rooftop::HookCalls base.send(:add_to_hook, :after_find, ->(r) { # basic content is the stuff which comes from WP by default. if r.respond_to?(:content) basic_fields = r.content[:basic].collect {|k,v| {name: k, value: v, fieldset: "Basic"}} # advanced fields from from ACF, and are exposed in the api in this form: # [ # { # "title"=>"The fieldset title", # "fields"=>[ # {"name"=>"field name", "label"=>"display name", "class"=>"type of field", "value"=>"The value of the field"}, # {"name"=>"field name", "label"=>"display name", "class"=>"type of field", # "value"=>"The value of the field"}, # etc. # ] # } # ] # Given that's a bit convoluted, we get both the content types into the same output format, like this: # {"field name", "label"=>"display name", "class"=>"type of field", "value"=>"value of the field", "fieldset"=>"fieldset if there is one, or Basic for the builtin ones"} advanced_fields = r.content[:advanced].collect do |fieldset| fieldset[:fields].each do |field| field.merge!(fieldset: fieldset[:title]) if field[:class].present? field[:type] = field[:class] end field.delete(:class) end fieldset[:fields] end advanced_fields.flatten! r.fields = Rooftop::Content::Collection.new((basic_fields + advanced_fields)) end }) base.send(:before_save, ->(r) { r.restore_fields! unless r.new? #TODO we need to write these back into the actual fields. }) end |
Instance Method Details
#has_field?(name, comparison = nil) ⇒ Boolean
test whether an instance has a field by name. Accepts a second argument if you want to test either the string value or the class.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rooftop/content/content_fields.rb', line 48 def has_field?(name, comparison=nil) has_field = fields.respond_to?(name.to_sym) if comparison.present? && comparison.is_a?(String) has_field && (fields.send(name.to_sym) == comparison) elsif comparison.present? has_field && fields.send(name.to_sym).is_a?(comparison) else has_field end end |