Skip to content

Commit 89dbdd7

Browse files
committed
Merge pull request #106 from ryanhertz/sorts
Sorts
2 parents 35f4f87 + 212d73e commit 89dbdd7

File tree

7 files changed

+99
-12
lines changed

7 files changed

+99
-12
lines changed

lib/spark_api/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
require 'spark_api/models/saved_search'
3131
require 'spark_api/models/search_template/quick_search'
3232
require 'spark_api/models/shared_listing'
33+
require 'spark_api/models/sort'
3334
require 'spark_api/models/standard_fields'
3435
require 'spark_api/models/system_info'
3536
require 'spark_api/models/tour_of_home'

lib/spark_api/models/base.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ def to_param
129129
attributes['Id']
130130
end
131131

132+
def to_partial_path
133+
"#{underscore(resource_pluralized)}/#{underscore(self.class.name.split('::').last)}"
134+
end
135+
136+
# can be overridden
137+
def resource_pluralized
138+
resource = self.class.name.split('::').last
139+
unless resource.split('').last == "s"
140+
resource = resource + "s"
141+
end
142+
resource
143+
end
144+
132145
protected
133146

134147
def write_attribute(attribute, value)
@@ -139,6 +152,16 @@ def write_attribute(attribute, value)
139152
end
140153
end
141154

155+
private
156+
157+
def underscore(string)
158+
string.to_s.gsub(/::/, '/').
159+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
160+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
161+
tr("-", "_").
162+
downcase
163+
end
164+
142165
end
143166
end
144167
end

lib/spark_api/models/concerns/savable.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ def update_attributes_after_create(result)
5959
attributes.merge! result
6060
end
6161

62-
# can be overridden
63-
def resource_pluralized
64-
resource = self.class.name.split('::').last
65-
unless resource.split('').last == "s"
66-
resource = resource + "s"
67-
end
68-
resource
69-
end
70-
7162
end
7263

7364
end

lib/spark_api/models/sort.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module SparkApi
2+
module Models
3+
class Sort < Base
4+
extend Finders
5+
6+
self.element_name="/sorts"
7+
self.prefix="/searchtemplates"
8+
9+
end
10+
end
11+
end

spec/fixtures/sorts/get.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"D": {
3+
"Results": [
4+
{
5+
"ResourceUri": "/v1/searchtemplates/sorts/20120717212004874996000000",
6+
"Id": "20130717212004874996000000",
7+
"Name": "My Custom Listing Sort",
8+
"OwnerId": "20000426173054342350000000",
9+
"MlsId": "20000426143505724628000000",
10+
"Inheritable": true,
11+
"Inherited": false,
12+
"Fields": [
13+
{
14+
"Domain": "StandardFields",
15+
"GroupField": null,
16+
"Field": "ListPrice",
17+
"SortType": "Ascending"
18+
},
19+
{
20+
"Domain": "StandardFields",
21+
"GroupField": null,
22+
"Field": "MlsStatus",
23+
"SortType": "Descending"
24+
}
25+
],
26+
"ModificationTimestamp": "2013-07-09T15:31:47Z"
27+
}
28+
],
29+
"Success": true
30+
}
31+
}

spec/unit/spark_api/models/base_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
require './spec/spec_helper'
22

33
# Sample resource models for testing the base class
4-
class MyExampleModel < Base
5-
self.element_name = "example"
6-
self.prefix = "/test/"
4+
module SparkApi
5+
module Models
6+
class MyExampleModel < Base
7+
self.element_name = "example"
8+
self.prefix = "/test/"
9+
end
10+
end
711
end
812

913
class MyDefaultModel < Base
@@ -153,4 +157,11 @@ class MyDefaultModel < Base
153157

154158
end
155159

160+
describe "to_partial_path" do
161+
it "should return the partial path" do
162+
model = MyExampleModel.new()
163+
model.to_partial_path.should eq("my_example_models/my_example_model")
164+
end
165+
end
166+
156167
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require './spec/spec_helper'
2+
3+
describe Sort do
4+
before(:each) do
5+
stub_auth_request
6+
end
7+
8+
it "should include the finders module" do
9+
Sort.should respond_to(:find)
10+
end
11+
12+
it "should return sorts" do
13+
stub_api_get("/searchtemplates/sorts", 'sorts/get.json')
14+
sorts = Sort.find(:all)
15+
sorts.should be_an(Array)
16+
sorts.length.should eq(1)
17+
end
18+
19+
end

0 commit comments

Comments
 (0)