Effective Questions
Underlying questions and responses module for use with polls, cpd audits and more.
An admin creates an acts_as_questionable resource with one or more questions.
The responses are collected by an acts_as_responsable resource with one or more responses
Works with action_text for content bodies, and active_storage for file uploads.
Getting Started
This requires Rails 6 and Twitter Bootstrap 4 and just works with Devise.
Please first install the effective_datatables gem.
Please download and install the Twitter Bootstrap4
Add to your Gemfile:
gem 'haml'
gem 'effective_questions'Run the bundle command to install it:
bundle installThen run the generator:
rails generate effective_questions:installThe generator will install an initializer which describes all configuration options and creates a database migration.
If you want to tweak the table names, manually adjust both the configuration file and the migration now.
Then migrate the database:
rake db:migrateSet up your permissions:
# Regular signed up user. Guest users not supported.
if user.persisted?
end
if user.admin?
endUsage
You can render the results with ``.
Authorization
All authorization checks are handled via the config.authorization_method found in the app/config/initializers/effective_questions.rb file.
It is intended for flow through to CanCan or Pundit, but neither of those gems are required.
This method is called by all controller actions with the appropriate action and resource
Action will be one of [:index, :show, :new, :create, :edit, :update, :destroy]
Resource will the appropriate object or class
The authorization method is defined in the initializer file:
# As a Proc (with CanCan)
config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) }# As a Custom Method
config.authorization_method = :my_authorization_methodand then in your application_controller.rb:
def my_authorization_method(action, resource)
current_user.is?(:admin) || EffectivePunditPolicy.new(current_user, resource).send('#{action}?')
endor disabled entirely:
config.authorization_method = falseIf the method or proc returns false (user is not authorized) an Effective::AccessDenied exception will be raised
You can rescue from this exception by adding the following to your application_controller.rb:
rescue_from Effective::AccessDenied do |exception|
respond_to do |format|
format.html { render 'static_pages/access_denied', status: 403 }
format.any { render text: 'Access Denied', status: 403 }
end
endLicense
MIT License. Copyright Code and Effect Inc.
Testing
Run tests by:
rails testContributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Bonus points for test coverage
- Create new Pull Request