Skip to content
Browse files

Merge pull request #374 from ticktricktrack/destroy-with-params

Feature: Allow payload on destroy actions
  • Loading branch information...
2 parents b1f6c37 + 97ef04e commit 8fcb92ecac1bb5138baed81e36353eba8ba96f7a @hubert hubert committed Nov 13, 2015
Showing with 28 additions and 2 deletions.
  1. +3 −2 lib/her/model/orm.rb
  2. +25 −0 spec/model/orm_spec.rb
View
5 lib/her/model/orm.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
module Her
module Model
# This module adds ORM-like capabilities to the model
@@ -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]
View
25 spec/model/orm_spec.rb
@@ -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

0 comments on commit 8fcb92e

Please sign in to comment.
Something went wrong with that request. Please try again.