diff options
| author | Alban Peignier | 2017-12-20 21:30:22 +0100 |
|---|---|---|
| committer | GitHub | 2017-12-20 21:30:22 +0100 |
| commit | 6b4b00a57d2c96ea9f2c663dd9892ccdd4fdbd29 (patch) | |
| tree | a7f69ebd48e32bf14d2da6ba47c84a8e2e57aae2 /app/controllers/concerns/feature_checker.rb | |
| parent | 0a1e40a31c27c59018cbdb8821b530ccfd59b878 (diff) | |
| parent | 462c1c257e954c77f1dedfc770d3c78111c1b499 (diff) | |
| download | chouette-core-6b4b00a57d2c96ea9f2c663dd9892ccdd4fdbd29.tar.bz2 | |
Merge pull request #164 from af83/5339-organisation-and-features
Create Organisation#features and FeatureChecker. Refs #5339
Diffstat (limited to 'app/controllers/concerns/feature_checker.rb')
| -rw-r--r-- | app/controllers/concerns/feature_checker.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/app/controllers/concerns/feature_checker.rb b/app/controllers/concerns/feature_checker.rb new file mode 100644 index 000000000..9ca5ed0a7 --- /dev/null +++ b/app/controllers/concerns/feature_checker.rb @@ -0,0 +1,42 @@ +# Check availability of optional features +# +# In your controller, use : +# +# requires_feature :test +# requires_feature :test, only: [:show] +# +# In your view, use : +# +# has_feature? :test +# +module FeatureChecker + extend ActiveSupport::Concern + + module ClassMethods + def requires_feature(feature, options = {}) + before_action options do + check_feature! feature + end + end + end + + included do + helper_method :has_feature? + end + + protected + + def has_feature?(*features) + features.all? do |feature| + current_organisation.has_feature? feature + end + end + + def check_feature!(*features) + unless has_feature?(*features) + raise NotAuthorizedError, "Feature not autorized" + end + end + + class NotAuthorizedError < StandardError; end +end |
