Module: Zendesk2::Collection
- Included in:
- Brands, Categories, Forums, Groups, HelpCenter::Articles, HelpCenter::Categories, HelpCenter::Posts, HelpCenter::Sections, HelpCenter::Subscriptions, HelpCenter::Topics, HelpCenter::Translations, Memberships, Organizations, TicketAudits, TicketComments, TicketFields, TicketForms, Tickets, TopicComments, Topics, UserFields, UserIdentities, Users, Views
- Defined in:
- lib/zendesk2/collection.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#all(params = {}) ⇒ Object
Fetch a collection of resources.
- #collection_method ⇒ Object
- #collection_root ⇒ Object
-
#create(attributes = {}) ⇒ Cistern::Model, FalseClass
Quietly attempt creation of resource.
-
#create!(attributes = {}) ⇒ Cistern::Model
Attempt creation of resource and explode if unsuccessful.
-
#get(*args) ⇒ Zendesk2::Model, NilClass
Quiet version of #get!.
-
#get!(identity_or_hash) ⇒ Zendesk2::Model
Fetch a single of resource.
- #model_method ⇒ Object
- #model_root ⇒ Object
- #namespace ⇒ Object
- #new(attributes = {}) ⇒ Object
Class Method Details
.cistern_included ⇒ Object
4 |
# File 'lib/zendesk2/collection.rb', line 4 alias cistern_included included |
.included(receiver) ⇒ Object
6 7 8 9 10 |
# File 'lib/zendesk2/collection.rb', line 6 def included(receiver) cistern_included(receiver) receiver.extend(ClassMethods) super end |
Instance Method Details
#all(params = {}) ⇒ Object
Fetch a collection of resources
65 66 67 68 69 70 71 72 |
# File 'lib/zendesk2/collection.rb', line 65 def all(params = {}) scoped_attributes = self.class.scopes.inject({}) { |a, e| a.merge(e.to_s => public_send(e)) }.merge(params) body = cistern.send(collection_method, scoped_attributes).body load(body[collection_root]) merge_attributes(Cistern::Hash.slice(body, 'count')) self end |
#collection_method ⇒ Object
28 29 30 |
# File 'lib/zendesk2/collection.rb', line 28 def collection_method self.class.collection_method end |
#collection_root ⇒ Object
32 33 34 |
# File 'lib/zendesk2/collection.rb', line 32 def collection_root self.class.collection_root end |
#create(attributes = {}) ⇒ Cistern::Model, FalseClass
Quietly attempt creation of resource. Check #new_record? and #errors for success
59 60 61 62 |
# File 'lib/zendesk2/collection.rb', line 59 def create(attributes = {}) model = new(attributes.merge(Zendesk2.stringify_keys(self.attributes))) model.save end |
#create!(attributes = {}) ⇒ Cistern::Model
Attempt creation of resource and explode if unsuccessful
51 52 53 54 |
# File 'lib/zendesk2/collection.rb', line 51 def create!(attributes = {}) model = new(attributes.merge(Zendesk2.stringify_keys(self.attributes))) model.save! end |
#get(*args) ⇒ Zendesk2::Model, NilClass
Quiet version of #get!
106 107 108 109 110 |
# File 'lib/zendesk2/collection.rb', line 106 def get(*args) get!(*args) rescue Zendesk2::Error nil end |
#get!(identity) ⇒ Zendesk2::Model #get!(scope) ⇒ Zendesk2::Model
Fetch a single of resource
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/zendesk2/collection.rb', line 88 def get!(identity_or_hash) scoped_attributes = self.class.scopes.inject({}) { |a, e| a.merge(e.to_s => public_send(e)) } if identity_or_hash.is_a?(Hash) scoped_attributes.merge!(identity_or_hash) else scoped_attributes['id'] = identity_or_hash end scoped_attributes = { namespace => scoped_attributes } data = cistern.send(model_method, scoped_attributes).body[model_root] new(data) if data end |
#model_method ⇒ Object
36 37 38 |
# File 'lib/zendesk2/collection.rb', line 36 def model_method self.class.model_method end |
#model_root ⇒ Object
40 41 42 |
# File 'lib/zendesk2/collection.rb', line 40 def model_root self.class.model_root end |
#namespace ⇒ Object
44 45 46 |
# File 'lib/zendesk2/collection.rb', line 44 def namespace self.class.namespace end |
#new(attributes = {}) ⇒ Object
112 113 114 |
# File 'lib/zendesk2/collection.rb', line 112 def new(attributes = {}) super(self.attributes.merge(attributes)) end |