has_prerequisite 1.0.0.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6bf73429257d857c3468aa9f0b6dfc0b3a80ac2
4
+ data.tar.gz: adf3d755c211db9bf86f13c649f62628ffbed012
5
+ SHA512:
6
+ metadata.gz: af9b3bf96d8aec2147a6770f103ced6e2a5d044429723d332b135f3cd6d59a98ec0e6cb9168e0f7043c8fa7952e34a308ea9ad0ef2f4042b715a70c0de17995f
7
+ data.tar.gz: 2247e6c7d0578d9ae4691d665757573da594e4af99ba12b2ffd9c145f8014ce5eed95ab610332abd80005ead6af3958a23cc11ff94888e08ccfd577713a0c9db
@@ -0,0 +1,101 @@
1
+ # HasPrerequisite
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/has_prerequisite`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'has_prerequisite'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install has_prerequisite
22
+
23
+ ## Usage
24
+
25
+ This gem adds features to your Rails controllers.
26
+
27
+ First, in your `ApplicationController` or any controller or method that you want to protect, use `perform_checks` method.
28
+
29
+ ```Ruby
30
+ class ApplicationController < ActionController::Base
31
+
32
+ before_filter :perform_checks # This will execute on every method of your controller
33
+
34
+ def index
35
+ preform_checks # you can call it directly here, especially if you need an instance variable instanciated for a prerequisite
36
+ end
37
+ ```
38
+
39
+ Once you made sure the checks will be performed on the parts of your application you want to protect, define your prerequisites where you want to protect. The `redirection_path` is where you want your user to be redirected to fulfill the prerequisite. Current location will be stored for future redirection, when the prerequisite is fulfilled.
40
+
41
+ ``` Ruby
42
+ class ProfilesController < ApplicationController
43
+ has_prerequisite :accepted_terms_and_conditions, redirection_path :terms_and_conditions_path
44
+
45
+ private
46
+
47
+ def accepted_terms_and_conditions
48
+ current_user.accepted_terms_and_conditions_at.present?
49
+ end
50
+ end
51
+ ```
52
+
53
+ If the path to fulfill the prerequisite is protected by `has_prerequisite` and `perform_checks` (if you're using them in the `ApplicationController` for instance), you can mark it as `fulfilling_prerequisite`
54
+
55
+ ``` Ruby
56
+ class TermsAndConditionsController < ApplicationController
57
+ fulfilling_prerequisite
58
+ end
59
+ ```
60
+
61
+ Once action was taken by the user and can now access the page they wanted to see at the first place, you can use `step_fulfilled!` to redirect them back to it
62
+
63
+ ``` Ruby
64
+ class TermsAndConditionsController < ApplicationController
65
+ fulfilling_prerequisite
66
+
67
+ def edit; end
68
+
69
+ def update
70
+ if current_user.update(accepted_terms_and_conditions_at: Datetime.now)
71
+ step_fulfilled!
72
+ else
73
+ render :edit
74
+ end
75
+ end
76
+ end
77
+ ```
78
+
79
+ ### Options
80
+
81
+ You can use a conditionnal to the prerequisite
82
+
83
+ ``` Ruby
84
+ prerequisite :valid_subscription, redirection_path: :new_subscription_path, if: :should_be_charged?
85
+ ```
86
+
87
+ ## Development
88
+
89
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
90
+
91
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sophiedeziel/has_prerequisite. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
96
+
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
101
+
@@ -0,0 +1,27 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ require 'rdoc/task'
11
+
12
+ RDoc::Task.new(:rdoc) do |rdoc|
13
+ rdoc.rdoc_dir = 'rdoc'
14
+ rdoc.title = 'HasPrerequisite'
15
+ rdoc.options << '--line-numbers'
16
+ rdoc.rdoc_files.include('README.md')
17
+ rdoc.rdoc_files.include('lib/**/*.rb')
18
+ end
19
+
20
+
21
+
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ task :default => :spec
@@ -0,0 +1,63 @@
1
+ require "has_prerequisite/version"
2
+ require "has_prerequisite/railtie" if defined?(Rails)
3
+ require "active_support"
4
+
5
+ module HasPrerequisite
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :prerequisites, :skipping_checks
10
+ self.prerequisites = []
11
+ self.skipping_checks = false
12
+
13
+ rescue_from PrerequisiteNotMet, with: :prerequisite_not_met!
14
+ end
15
+
16
+ module ClassMethods
17
+ def prerequisite(method, redirection_path: nil, **options)
18
+ prerequisites << { method: method, redirection_path: redirection_path, options: options }
19
+ end
20
+
21
+ def fulfilling_prerequisite
22
+ self.skipping_checks = true
23
+ end
24
+ end
25
+
26
+ def step_fulfilled!
27
+ redirect_to stored_location
28
+ end
29
+
30
+ private
31
+
32
+ def perform_checks
33
+ return if self.class.skipping_checks
34
+ return unless failing_preriquisite
35
+ store_location(request.fullpath)
36
+ raise PrerequisiteNotMet
37
+ end
38
+
39
+ def failing_preriquisite
40
+ @failing_preriquisite ||= prerequisites.find do |p|
41
+ !send(p[:method]) if p[:options][:if].nil? || send(p[:options][:if])
42
+ end
43
+ end
44
+
45
+ def prerequisite_not_met!
46
+ redirect_to redirect_path
47
+ end
48
+
49
+ def redirect_path
50
+ return send(failing_preriquisite[:redirection_path]) if failing_preriquisite[:redirection_path].is_a? Symbol
51
+ failing_preriquisite[:redirection_path]
52
+ end
53
+
54
+ def store_location(path)
55
+ session[:return_to] = path
56
+ end
57
+
58
+ def stored_location
59
+ session.delete(:return_to) || root_path
60
+ end
61
+
62
+ class PrerequisiteNotMet < StandardError; end
63
+ end
@@ -0,0 +1,9 @@
1
+ module HasPrerequisite
2
+ class Railtie < Rails::Railtie
3
+ initializer "has_prerequisite.configure_view_controller" do |app|
4
+ ActiveSupport.on_load :action_controller do
5
+ include HasPrerequisite
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module HasPrerequisite
2
+ VERSION = '1.0.0.alpha'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :has_prerequisite do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_prerequisite
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Sophie Déziel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Simple authorization method to redirect to specific pages when a prerequisite
42
+ is not met.
43
+ email:
44
+ - courrier@sophiedeziel.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - README.md
50
+ - Rakefile
51
+ - lib/has_prerequisite.rb
52
+ - lib/has_prerequisite/railtie.rb
53
+ - lib/has_prerequisite/version.rb
54
+ - lib/tasks/has_prerequisite_tasks.rake
55
+ homepage: http://github.com/sophiedeziel/has_prerequisite
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">"
71
+ - !ruby/object:Gem::Version
72
+ version: 1.3.1
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.5.1
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Simple authorization method to redirect to specific pages when a prerequisite
79
+ is not met.
80
+ test_files: []