Skip to content

Commit edeb226

Browse files
committed
Refactored initializer
1 parent 6d3c7ef commit edeb226

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

lib/zendesk_api/actions.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ def find!(client, options = {})
107107
yield req if block_given?
108108
end
109109

110-
new(client).tap do |resource|
111-
resource.handle_response(response)
112-
resource.set_includes(resource, includes, response.body)
113-
resource.attributes.clear_changes
114-
end
110+
new_from_response(client, response, includes)
115111
end
116112

117113
# Finds, returning nil if it fails
@@ -157,10 +153,7 @@ def create_many!(client, attributes_array)
157153
yield req if block_given?
158154
end
159155

160-
JobStatus.new(client).tap do |resource|
161-
resource.handle_response(response)
162-
resource.attributes.clear_changes
163-
end
156+
JobStatus.new_from_response(client, response)
164157
end
165158
end
166159

@@ -218,10 +211,7 @@ def destroy_many!(client, ids)
218211
yield req if block_given?
219212
end
220213

221-
JobStatus.new(client).tap do |resource|
222-
resource.handle_response(response)
223-
resource.attributes.clear_changes
224-
end
214+
JobStatus.new_from_response(client, response)
225215
end
226216
end
227217

lib/zendesk_api/resource.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ def initialize(client, attributes = {})
6666
@attributes.clear_changes unless new_record?
6767
end
6868

69+
def self.new_from_response(client, response, includes = nil)
70+
new(client).tap do |resource|
71+
resource.handle_response(response)
72+
resource.set_includes(resource, includes, response.body) if includes
73+
resource.attributes.clear_changes
74+
end
75+
end
76+
6977
# Passes the method onto the attributes hash.
7078
# If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash.
7179
def method_missing(*args, &block)

0 commit comments

Comments
 (0)