Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to subclass the Insightly models #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/insightly/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def self.custom_fields(*args)
next if method.nil? or method == ""
method_name = method.to_s.downcase.to_sym
send :define_method, method_name do
@data["#{self.class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"]
@data["#{base_insightly_class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"]
end
method_name = "#{method.to_s.downcase}=".to_sym
send :define_method, method_name do |value|
@data["#{self.class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] = value
@data["#{base_insightly_class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] = value
end
end
end
Expand Down Expand Up @@ -57,14 +57,20 @@ def initialize(id = nil)
load(id) if id
end

def base_insightly_class
self.class.ancestors.select do |anc|
anc.name.split("::").first == "Insightly"
end.first
end

def url_base
self.class.url_base
base_insightly_class.url_base
end

def remote_id_field
return self.class.remote_id_field if self.class.remote_id_field
self.class.to_s.downcase.gsub("insightly::", "") + "_id"
return base_insightly_class.remote_id_field if base_insightly_class.remote_id_field

base_insightly_class.name.demodulize.downcase + "_id"
end

def remote_id
Expand Down
25 changes: 17 additions & 8 deletions lib/insightly/read_write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ def post_collection(path, params, content_selector = :json)
else
content_type = content_selector
end
response = RestClient::Request.new(:method => :post,
:url => "#{config.endpoint}/#{path.to_s}",
:user => config.api_key,
:password => "",
:payload => params,
:headers => {:accept => content_type, :content_type => content_type}).execute
process(response, content_selector)

begin
url = "#{config.endpoint}/#{path.to_s}"

response = RestClient::Request.new(:method => :post,
:url => url,
:user => config.api_key,
:password => "",
:payload => params,
:headers => {:accept => content_type, :content_type => content_type}).execute

process(response, content_selector)
rescue => e
puts "failure talking to Insight.ly #{url}"
raise e
end
end

def put_collection(path, params, content_selector = :json)
Expand All @@ -47,4 +56,4 @@ def put_collection(path, params, content_selector = :json)
process(response, content_selector)
end
end
end
end
27 changes: 26 additions & 1 deletion spec/unit/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,29 @@
describe Insightly::Base do
before(:each) do
end
end

context "url_base" do
it "uses the base_insightly_class's" do
base = Insightly::Base.new
base_class = double()
url = "some url"
base_class.should_receive(:url_base).and_return(url)
base.should_receive(:base_insightly_class).and_return(base_class)
base.url_base.should eql(url)
end
end

context "subclass" do
before(:all) do
class TestTask < Insightly::Task
end
end
it "knows the base class's url" do
TestTask.new.url_base.should eql(Insightly::Task.new.url_base)
end

it "knows the base class's remote_id_field" do
TestTask.new.remote_id_field.should eql(Insightly::Task.new.remote_id_field)
end
end
end