Skip to content

Commit

Permalink
Merge pull request #374 from ticktricktrack/destroy-with-params
Browse files Browse the repository at this point in the history
Feature: Allow payload on destroy actions
  • Loading branch information
hubert committed Nov 13, 2015
2 parents b1f6c37 + 97ef04e commit 8fcb92e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/her/model/orm.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding: utf-8
module Her
module Model
# This module adds ORM-like capabilities to the model
Expand Down Expand Up @@ -72,10 +73,10 @@ def save!
# @user = User.find(1)
# @user.destroy
# # Called via DELETE "/users/1"
def destroy
def destroy(params = {})
method = self.class.method_for(:destroy)
run_callbacks :destroy do
self.class.request(:_method => method, :_path => request_path) do |parsed_data, response|
self.class.request(params.merge(:_method => method, :_path => request_path)) do |parsed_data, response|
assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any?
@metadata = parsed_data[:metadata]
@response_errors = parsed_data[:errors]
Expand Down
25 changes: 25 additions & 0 deletions spec/model/orm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,31 @@ def friends
@user.active.should be_falsey
@user.should be_destroyed
end

context "with params" do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.delete("/users/1?delete_type=soft") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke", :active => false }.to_json] }
end
end
end

it "handle resource deletion through the .destroy class method" do
@user = Foo::User.destroy_existing(1, delete_type: 'soft')
@user.active.should be_false
@user.should be_destroyed
end

it "handle resource deletion through #destroy on an existing resource" do
@user = Foo::User.find(1)
@user.destroy(delete_type: 'soft')
@user.active.should be_false
@user.should be_destroyed
end
end
end

context 'customizing HTTP methods' do
Expand Down

0 comments on commit 8fcb92e

Please sign in to comment.