Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Customs uses the power of cancancan (formerly known as cancan) to control the flow of your controllers.
It adds some magic in your rails controllers, through the cancan magic formula : load_and_authorize_resource
, and let you customize the flow.
Customs provides you:
class DrogsController < ApplicationController
control_and_rescue_traffic
respond_to :html, :json
load_and_authorize_resource :drog
end
In the given examples, we do not endorse in any way the traffic of illegal products. Customs is watching you!
Add this line to your application's Gemfile:
gem 'customs'
Or install it yourself as:
$ gem install customs
Then, take a beer.
Two methods available:
control_traffic
rescue_traffic
Or only one to bind them:
control_and_rescue_traffic
class DrogsController < ApplicationController
load_and_authorize_resource :drog
control_and_rescue_traffic
respond_to :html, :json
end
control_traffic
provides you default methods to list, create, update & delete resources, depending on the cancan load_resource
arguments.
It uses exception to control the flow of data, such as:
rescue_traffic
provides methods for specific HTTP statuses & routes the flow exceptions to the most appropriate one.
unauthorized
forbidden
not_found
unprocessable
Callbacks are working like rails filters :
before_save :make_something
before_save :make_something_else, only: :create
before_destroy :make_nothing
class BaggagesController < ApplicationController
load_and_authorize_resource :baggage
before_save :add_illegal_content
after_save :report_to_autorithies
after_destroy :analyse_scraps
end
Maybe you prefer to pass a block to create
, update
or destroy
?
In this case, the block is called after the save!
method.
class BaggagesController < ApplicationController
load_and_authorize_resource :baggage
def create
super do
control_content && report_to_autorithies
end
end
end
You can customize the controller flow by overriding any following methods:
resource_params
- parameters attributes, which will be used to create/update resourcessuccess_response
- what happened after successfull actionresource_location
- where redirect on a success response (depending on the action_name
)For a better understanding, you have to keep in mind that control_traffic
apply these methods with the following schema:
class DrogsController < ApplicationController
def create
resource.assign_attributes resource_params
resource.save!
success_response
end
def update
resource.assign_attributes resource_params
resource.save!
success_response
end
def destroy
resource.destroy
success_response
end
protected
def resource_params
params[resource_name]
end
def success_response
respond_with resource, :location => resource_location
end
def resource_location
case action_name
when 'create' then resource
when 'update' then resource
when 'destroy' then resource_name.to_s.pluralize.to_sym
end
end
end
I highly recommend overriding the resource_params
method with Strong Parameters:
class SmugglersController < ApplicationController
load_and_authorize_resource :smuggler
protected
def resource_params
params.require(:smuggler).permit(:name, :skills)
end
end
With rescue_traffic
, rescued errors will output something like that in you logs:
[Customs] Rescuing from ActionView::MissingTemplate with :not_found
If you need to trace the error, you just have to override the rescue_exception_with
method:
def rescue_exception_with exception, method
logger.error "Rescuing from #{ exception.class } with :#{ method }"
exception.backtrace[0..10].each {|line| logger.error line }
send method
end
git checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that customs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.