Skip to content

Commit

Permalink
add decorators; add scope to current_user to rails app that use devis…
Browse files Browse the repository at this point in the history
…e with another scoped
  • Loading branch information
altherlex authored and iffyuva committed Apr 22, 2017
1 parent eddc517 commit 8f5d6ca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*.gem
*.rbc
.DS_Store
**/*/.DS_Store
.bundle
.config
.yardoc
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ Typical implementation would be:
If you are using authentication gems like devise, you get `current_user` for free
and you don't have to define it.

### Override

Override path to redirect after answer the survey

```ruby
# my_app/app/decorators/controllers/rapidfire/answer_groups_controller_decorator.rb
Rapidfire::AnswerGroupsController.class_eval do
def after_answer_path_for
main_app.root_path
end
end
```

### Routes Information
Once this gem is mounted on, say at 'rapidfire', it generates several routes
You can see them by running `bundle exec rake routes`.
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/rapidfire/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,15 @@ def authenticate_administrator!
raise Rapidfire::AccessDenied.new("cannot administer questions")
end
end

# Override prefixes to consider the scoped.
# for method current_user
def scoped
:user
end

def current_user
send 'current_'+scoped.to_s
end
end
end
19 changes: 18 additions & 1 deletion app/controllers/rapidfire/attempts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ def create
@attempt_builder = AttemptBuilder.new(attempt_params)

if @attempt_builder.save
redirect_to surveys_path
redirect_to after_answer_path_for
else
render :new
end
end

private

def find_survey!
@survey = Survey.find(params[:survey_id])
end
Expand All @@ -25,5 +26,21 @@ def attempt_params
answer_params = { params: (params[:attempt] || {}) }
answer_params.merge(user: current_user, survey: @survey)
end

# Override path to redirect after answer the survey
# Write:
# # my_app/app/decorators/controllers/rapidfire/answer_groups_controller_decorator.rb
# Rapidfire::AnswerGroupsController.class_eval do
# def after_answer_path_for
# main_app.root_path
# end
# end
def after_answer_path_for
question_groups_path
end

def rapidfire_current_scoped
send 'current_'+scoped.to_s
end
end
end
7 changes: 7 additions & 0 deletions lib/rapidfire/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

module Rapidfire
class Engine < ::Rails::Engine
# engine_name 'rapidfire'
isolate_namespace Rapidfire

config.to_prepare do
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
require_dependency(c)
end
end
end
end

0 comments on commit 8f5d6ca

Please sign in to comment.