Skip to content

Commit

Permalink
Override RAW attributes
Browse files Browse the repository at this point in the history
the  rewrites integer based attributes into attribute_id. This PR rewrites the raw values returned from
an API response into the correct '_id' style

https://www.pivotaltracker.com/story/show/140521307
  • Loading branch information
syncrou committed Feb 23, 2017
1 parent b7cf3d6 commit ef521ad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/ansible_tower_client/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ def self.create(*args)
def update_attributes!(attributes)
@api.patch(url, attributes.to_json)
attributes.each do |method_name, value|
send("#{method_name}=", value)
if override_raw_attributes[method_name]
send("#{override_raw_attributes[method_name]}=", value)
else
send("#{method_name}=", value)
end
end
true
end
Expand Down Expand Up @@ -145,6 +149,10 @@ def destroy
false
end

def override_raw_attributes
{}
end

def hashify(attribute)
YAML.safe_load(send(attribute))
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ansible_tower_client/base_models/job_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ def survey_spec_hash
def extra_vars_hash
extra_vars.empty? ? {} : hashify(:extra_vars)
end

def override_raw_attributes
{ :credential => :credential_id, :inventory => :inventory_id, :project => :project_id }
end
end
end
12 changes: 12 additions & 0 deletions spec/job_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@
described_class.new(api, raw_instance).launch(json_with_limit)
end
end

context 'override_raw_attributes' do
let(:obj) { described_class.new(instance_double("Faraday::Connection"), raw_instance) }
let(:instance_api) { obj.instance_variable_get(:@api) }

it 'translates :project to :project_id for update_attributes' do
raw_instance[:project_id] = 10
expect(instance_api).to receive(:patch).and_return(instance_double("Faraday::Result", :body => raw_instance.to_json))
expect(obj.update_attributes(:project => '5')).to eq true
expect(obj.project_id).to eq '5'
end
end
end

0 comments on commit ef521ad

Please sign in to comment.