simon_says 0.0.27b6 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19de05c0bda5120ccdd27df9f861c3d436982c25
4
- data.tar.gz: 21ff0c7d067c9edbfeabf8bfb83a6b9be301e183
3
+ metadata.gz: 685f3f8f7cedb32fba2e7b051480920658a8da7e
4
+ data.tar.gz: a09f1909ec655d6653737c4bf1fd78a9e3c7d8d8
5
5
  SHA512:
6
- metadata.gz: 0fdb85e2380e4505ddcc07e0106f83260d41224021a96411b58bdd162a21c94c2cc2c5a2d97dfb144364ddb342c0e0873f5e7b9c1006d4a9cca16889cf22e558
7
- data.tar.gz: 7ae83204bb804a8e9a0fbbe75582c57644a2e60c9a5fab752014c1ffdf011f292bd738f842152a2c65bdb5ae7c5e6baa214b1c7634397e6eb584d49407e644b6
6
+ metadata.gz: f4f436bc143a52f7ca03a90335fb227b1aa9794f1ed462c9c53cb5b26263538f603b23d37ec4cf4a5d7a7a8a192931d7b635d518bf5d28390b624cfca591ce08
7
+ data.tar.gz: 0f677b3f2830329eb4765e082321a787e5d2037bad8b72f79af7471bff238c8604b7fc1f0a0a0c631c15787f45fbcbfcffb76e28b18c88f4e8a98c920a1efc7b
data/.gitignore CHANGED
@@ -12,5 +12,8 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ .ruby-version
16
+
17
+ *.gem
15
18
 
16
19
  /test/rails_app/db/*.sqlite3
data/.travis.yml CHANGED
@@ -1,3 +1,11 @@
1
1
  language: ruby
2
+ cache: bundler
3
+ install: bundle install --jobs=3 --retry=3
2
4
  rvm:
3
- - 2.1.3
5
+ - "2.2.2"
6
+ before_script:
7
+ - cd test/rails_app/
8
+ - RAILS_ENV=test bundle exec rake db:migrate db:fixtures:load
9
+ - cd ../..
10
+ script:
11
+ - bundle exec rake test
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
+ ruby '2.2.3'
2
3
 
3
4
  # Specify your gem's dependencies in auth_lib.gemspec
4
5
  gemspec
@@ -9,5 +10,9 @@ group :development do
9
10
  gem 'rdoc'
10
11
 
11
12
  gem 'guard'
12
- gem 'guard-minitest'
13
+ gem 'guard-minitest', "2.3.2"
14
+ end
15
+
16
+ group :test do
17
+ gem "codeclimate-test-reporter", require: false
13
18
  end
data/README.md CHANGED
@@ -4,152 +4,10 @@
4
4
  Logo](https://raw.githubusercontent.com/SimplyBuilt/SimonSays/master/SimonSays.png)
5
5
 
6
6
  This gem is a simple, declarative, role-based access control system for Rails that
7
- works great with devise! Take a look at the
8
- [docs](http://simplybuilt.github.io/SimonSays) for more details!
7
+ works great with devise! Take a look at the [website](http://simonsays.onsimplybuilt.com) or
8
+ [docs](http://www.rubydoc.info/github/SimplyBuilt/SimonSays/) for more details!
9
9
 
10
- ## Installation
11
-
12
- Add this line to your application's Gemfile:
13
-
14
- ```ruby
15
- gem 'simon_says'
16
- ```
17
-
18
- And then execute:
19
-
20
- $ bundle
21
-
22
- Or install it yourself as:
23
-
24
- $ gem install simon_says
25
-
26
- ## Usage
27
-
28
- SimonSays consists of two parts. One is a model concern called
29
- `Roleable`. The other is a controller concern called `Authorizer`. The
30
- idea is that you give users some set of roles and find and authorize
31
- against those roles on a controller (and action) basis.
32
-
33
- ### Roleable
34
-
35
- First, we need to define roles. Generally speaking roles will exist on
36
- either User models or on relationship models (like a through model linking a
37
- User to another resource).
38
-
39
- Here's a quick example:
40
-
41
- class User < ActiveRecord::Base
42
- include SimonSays::Roleable
43
-
44
- has_roles :add, :edit, :delete
45
- end
46
-
47
- User can now have none or one more roles:
48
-
49
- User.new.roles
50
- => []
51
-
52
- User.new.tap { |u| u.roles = :add, :edit }.roles
53
- => [:add, :edit]
54
-
55
- The roles are stored as an integer. When using `Roleable` you need add a
56
- `roles_mask` column. Note that we do not have any generators for this yet.
57
- Feel free to fork add them!
58
-
59
- You can customize this attribute using the `:as` option. For example:
60
-
61
- class Admin < ActiveRecord::Base
62
- include SimonSays::Roleable
63
-
64
- has_roles :design, :support, :moderator, as: :access
65
- end
66
-
67
- Admin.new.access
68
- => []
69
-
70
- Admin.new(access: :support).access
71
- => [:support]
72
-
73
- You can also use `Roleable` on through models. For example:
74
-
75
- class Membership < ActiveRecord::Base
76
- include SimonSays::Roleable
77
-
78
- belongs_to :user
79
- belongs_to :document
80
-
81
- has_roles :download, :edit, :delete,
82
- end
83
-
84
- There will be several methods as well as a scope generated by
85
- calling `has_roles`. See below for more details on the methods
86
- generated by `Roleable`. Be sure to also checkout the
87
- [docs](http://simplybuilt.github.io/SimonSays/SimonSays/Roleable/ClassMethods.html)
88
- for more details!
89
-
90
- ### Authorizer
91
-
92
- Next up is the `Authorizer`. This concern provides several methods that
93
- can be used within your controllers to declaratively find resources and
94
- ensuring certain role-based conditions are met.
95
-
96
- *Please note*, certain assumptions are made with `Authorizer`. Building
97
- upon the above `User` and `Admin` models, `Authorizer` would assume
98
- there is a `current_user` and `current_admin`. If these models
99
- correspond to devise scopes this would be the case by default.
100
- Additionally there would need to a be an `authenticate_user!` and
101
- `authenticate_admin!` method, which devise provides as well.
102
-
103
- Eventually, we would like to see better customization around the
104
- authentication aspects. This library is intended to solve the problem of
105
- authorization and access control. It is not an authentication library.
106
-
107
- The first step is to include the concern within the
108
- `ApplicationController` and to configure the default authorization
109
- method:
110
-
111
- class ApplicationController < ActionController::Base
112
- include SimonSays::Authorizer
113
- end
114
-
115
- Let's start with an example; here we'll create a reports resource that
116
- only Admin's with support access to use. The roles are supplied within
117
- the `authorize_resource` method. Note that, multiple roles can be
118
- supplied; access is granted if one or more are met.
119
-
120
- # routes.rb
121
- # Reports resource for Admins
122
- resources :reports
123
-
124
- # app/controllers/reports_controller.rb
125
- class ReportsController < ApplicationController
126
- authorize_resource :admin, :support
127
- find_resource :report, except: [:index, :new, :create]
128
- end
129
-
130
- Here's another example using the `Membership` through model.
131
-
132
- # routes.rb
133
- resources :documents
134
-
135
- # app/controllers/documents_controller.rb
136
- class DocumentsController < ApplicationController
137
- authenticate :user
138
-
139
- find_and_authorize :documents, :edit, through: :memberships, only: [:edit, :update]
140
- find_and_authorize :documents, :delete, through: :memberships, only: :destroy
141
- end
142
-
143
- The document model will not be found if the membership relationship does
144
- not exist and an `ActiveRecord::NotFound` exception will be raised.
145
-
146
- If the membership record exists, but the role conditions are not met,
147
- `Authorizer` will raise a `Denied` exception.
148
-
149
- If the document is found and the user has the access to it. It will be
150
- set as the `@document` instance variable. Make sure to checkout the
151
- [docs](http://simplybuilt.github.io/SimonSays/SimonSays/Authorizer/ClassMethods.html)
152
- for more details!
10
+ ![Build Status](https://travis-ci.org/SimplyBuilt/SimonSays.svg)
153
11
 
154
12
  ## Contributing
155
13
 
@@ -2,7 +2,7 @@ module SimonSays
2
2
  module Authorizer
3
3
  extend ActiveSupport::Concern
4
4
 
5
- class Denied < Exception
5
+ class Denied < StandardError
6
6
  def initialize(as, required, actual)
7
7
  # TODO i18n for err message (as should be singluarized with 1 flag)
8
8
  super "Access denied; #{required * ', '} role is required. Current access is #{actual * ', '}"
@@ -17,7 +17,7 @@ module SimonSays
17
17
  # available to your controllers.
18
18
  module ClassMethods
19
19
  # Authentication convenience method (to keep things declarative).
20
- # This method just setups a +before_filter+
20
+ # This method just setups a +before_action+
21
21
  #
22
22
  # * +scope+ is a symbol or string and should correspond to some sort
23
23
  # of authentication scope (ie: +authenticate_user!+)
@@ -28,7 +28,7 @@ module SimonSays
28
28
  # authenticate :user, expect: :index
29
29
  #
30
30
  def authenticate(scope, opts = {})
31
- before_filter :"authenticate_#{scope}!", filter_options(opts)
31
+ before_action :"authenticate_#{scope}!", filter_options(opts)
32
32
  end
33
33
 
34
34
  # Find and authorize a resource.
@@ -43,7 +43,7 @@ module SimonSays
43
43
  def find_and_authorize(resource, *roles)
44
44
  opts = roles.extract_options!
45
45
 
46
- before_filter(filter_options(opts)) do
46
+ before_action(filter_options(opts)) do
47
47
  find_resource resource, opts
48
48
 
49
49
  authorize roles, opts unless roles.empty?
@@ -55,7 +55,7 @@ module SimonSays
55
55
  # * +resource+ the name of the resource to find
56
56
  # * +opts+ filter options
57
57
  def find_resource(resource, opts = {})
58
- before_filter(filter_options(opts)) do
58
+ before_action(filter_options(opts)) do
59
59
  find_resource resource, opts
60
60
  end
61
61
  end
@@ -70,13 +70,13 @@ module SimonSays
70
70
  # * +roles+ one or more role symbols
71
71
  # * the last argument may also be a filter options hash
72
72
  def authorize_resource(resource, *roles)
73
- before_filter(filter_options(roles.extract_options!)) do
73
+ before_action(filter_options(roles.extract_options!)) do
74
74
  authorize(roles, { resource: resource })
75
75
  end
76
76
  end
77
77
 
78
78
  def filter_options(options) # :nodoc:
79
- { except: options.delete(:except), only: options.delete(:only) }
79
+ { except: options.delete(:except), only: options.delete(:only), prepend: options.delete(:prepend) }
80
80
  end
81
81
  end
82
82
 
@@ -138,7 +138,8 @@ module SimonSays
138
138
  scope = send(self.class.default_authorization_scope)
139
139
 
140
140
  elsif options[:from]
141
- scope = instance_variable_get("@#{options[:from]}")
141
+ scope = instance_variable_get("@#{options[:from]}") || send(options[:from])
142
+
142
143
  else
143
144
  klass = (options[:class_name] || resource).to_s
144
145
  # TODO support array of namespaces?
@@ -76,6 +76,8 @@ module SimonSays
76
76
 
77
77
  def #{name}=(args)
78
78
  args = [args] unless Array === args
79
+
80
+ args.compact!
79
81
  args.map!(&:to_sym)
80
82
 
81
83
  self[:#{name}_mask] = (args & #{const}).map { |i| 2 ** #{const}.index(i) }.sum
@@ -99,7 +101,7 @@ module SimonSays
99
101
  # Declare a scope for finding records with a given role set
100
102
  # TODO support an array roles (must match ALL)
101
103
  scope "with_#{name}", ->(role) {
102
- where("(#{name}_mask & ?) > 0", 2**roles.index(role))
104
+ where("(#{name}_mask & ?) > 0", 2**roles.index(role.to_sym))
103
105
  }
104
106
  end
105
107
  end
@@ -1,3 +1,3 @@
1
1
  module SimonSays
2
- VERSION = "0.0.27b6"
2
+ VERSION = "0.1.0"
3
3
  end
data/simon_says.gemspec CHANGED
@@ -6,7 +6,7 @@ require 'simon_says/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "simon_says"
8
8
  spec.version = SimonSays::VERSION
9
- spec.authors = ["Michael Coyne"]
9
+ spec.authors = ["Michael Coyne", "Cameron Craig", "SimplyBuilt"]
10
10
  spec.email = ["mikeycgto@gmail.com"]
11
11
  spec.summary = %q{Light-weight, declarative authorization and access control for Rails}
12
12
  spec.description = %q{This gem is a simple, easy-to-use declarative role-based access control system for Rails}
@@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "activesupport", "~> 4.0"
21
+ spec.add_dependency "activesupport", ">= 4.0", "< 5.1"
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "bundler", "~> 1.9"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
- spec.add_development_dependency "rails", "~> 4.1"
25
+ spec.add_development_dependency "rails", ">= 4.0", "< 5.1"
26
+ spec.add_development_dependency "responders", "~> 2.0"
26
27
  spec.add_development_dependency "mocha", "~> 1.1"
27
28
  end
@@ -9,7 +9,7 @@ class Admin::ReportsControllerTest < ActionController::TestCase
9
9
  test "index with access" do
10
10
  @controller.current_admin = @support
11
11
 
12
- get :index, format: :json
12
+ get :index, params: { format: :json }
13
13
 
14
14
  assert_response :success
15
15
  end
@@ -18,7 +18,7 @@ class Admin::ReportsControllerTest < ActionController::TestCase
18
18
  @controller.current_admin = @marketing
19
19
 
20
20
  assert_raises SimonSays::Authorizer::Denied do
21
- get :index, format: :json
21
+ get :index, params: { format: :json }
22
22
  end
23
23
  end
24
24
 
@@ -26,7 +26,7 @@ class Admin::ReportsControllerTest < ActionController::TestCase
26
26
  @controller.current_admin = @support
27
27
 
28
28
  assert_difference 'Admin::Report.count' do
29
- post :create, report: { title: 'Test' }, format: :json
29
+ post :create, params: { report: { title: 'Test' }, format: :json }
30
30
  end
31
31
  end
32
32
 
@@ -34,16 +34,15 @@ class Admin::ReportsControllerTest < ActionController::TestCase
34
34
  @controller.current_admin = @marketing
35
35
 
36
36
  assert_raises SimonSays::Authorizer::Denied do
37
- post :create, report: { title: 'Test' }, format: :json
37
+ post :create, params: { report: { title: 'Test' }, format: :json }
38
38
  end
39
39
  end
40
40
 
41
41
  test "show with access" do
42
42
  @controller.current_admin = @support
43
43
 
44
- get :show, id: admin_reports(:report_one), format: :json
44
+ get :show, params: { id: admin_reports(:report_one), format: :json }
45
45
 
46
- refute_nil assigns(:report)
47
46
  assert_response :success
48
47
  end
49
48
 
@@ -51,16 +50,15 @@ class Admin::ReportsControllerTest < ActionController::TestCase
51
50
  @controller.current_admin = @marketing
52
51
 
53
52
  assert_raises SimonSays::Authorizer::Denied do
54
- get :show, id: admin_reports(:report_one), format: :json
53
+ get :show, params: { id: admin_reports(:report_one), format: :json }
55
54
  end
56
55
  end
57
56
 
58
57
  test "update with access" do
59
58
  @controller.current_admin = @support
60
59
 
61
- patch :show, id: admin_reports(:report_one), report: { title: 'Test' }, format: :json
60
+ patch :show, params: { id: admin_reports(:report_one), report: { title: 'Test' }, format: :json }
62
61
 
63
- refute_nil assigns(:report)
64
62
  assert_response :success
65
63
  end
66
64
 
@@ -68,7 +66,7 @@ class Admin::ReportsControllerTest < ActionController::TestCase
68
66
  @controller.current_admin = @marketing
69
67
 
70
68
  assert_raises SimonSays::Authorizer::Denied do
71
- patch :show, id: admin_reports(:report_one), report: { title: 'Test' }, format: :json
69
+ patch :show, params: { id: admin_reports(:report_one), report: { title: 'Test' }, format: :json }
72
70
  end
73
71
  end
74
72
 
@@ -76,17 +74,15 @@ class Admin::ReportsControllerTest < ActionController::TestCase
76
74
  @controller.current_admin = @support
77
75
 
78
76
  assert_difference 'Admin::Report.count', -1 do
79
- delete :destroy, id: admin_reports(:report_one), format: :json
77
+ delete :destroy, params: { id: admin_reports(:report_one), format: :json }
80
78
  end
81
-
82
- refute_nil assigns(:report)
83
79
  end
84
80
 
85
81
  test "destroy without access" do
86
82
  @controller.current_admin = @marketing
87
83
 
88
84
  assert_raises SimonSays::Authorizer::Denied do
89
- delete :destroy, id: admin_reports(:report_one), format: :json
85
+ delete :destroy, params: { id: admin_reports(:report_one), format: :json }
90
86
  end
91
87
  end
92
88
  end
@@ -1,5 +1,5 @@
1
1
  require 'test_helper'
2
-
2
+ require 'pry'
3
3
  class DocumentsControllerTest < ActionController::TestCase
4
4
  setup do
5
5
  @alpha = documents(:alpha)
@@ -20,9 +20,8 @@ class DocumentsControllerTest < ActionController::TestCase
20
20
  test "show" do
21
21
  as_bob!
22
22
 
23
- get :show, id: @alpha.id, format: :json
23
+ get :show, params: { id: @alpha.id, format: "json" }
24
24
 
25
- refute_nil assigns(:document)
26
25
  assert_response :success
27
26
  end
28
27
 
@@ -30,16 +29,15 @@ class DocumentsControllerTest < ActionController::TestCase
30
29
  as_jim!
31
30
 
32
31
  assert_raises ActiveRecord::RecordNotFound do
33
- get :show, id: @alpha.id, format: :json
32
+ get :show, params: { id: @alpha.id, format: :json }
34
33
  end
35
34
  end
36
35
 
37
36
  test "update with access" do
38
37
  as_bob!
39
38
 
40
- patch :update, id: @alpha.id, document: { title: 'Test' }, format: :json
39
+ patch :update, params: { id: @alpha.id, document: { title: 'Test' }, format: :json }
41
40
 
42
- refute_nil assigns(:document)
43
41
  assert_response :success
44
42
  end
45
43
 
@@ -47,7 +45,7 @@ class DocumentsControllerTest < ActionController::TestCase
47
45
  as_bob!
48
46
 
49
47
  assert_raises SimonSays::Authorizer::Denied do
50
- patch :update, id: @beta.id, document: { title: 'Test' }, format: :json
48
+ patch :update, params: { id: @beta.id, document: { title: 'Test' }, format: :json }
51
49
  end
52
50
  end
53
51
 
@@ -55,7 +53,7 @@ class DocumentsControllerTest < ActionController::TestCase
55
53
  as_jim!
56
54
 
57
55
  assert_raises ActiveRecord::RecordNotFound do
58
- patch :update, id: @alpha.id, document: { title: 'Test' }, format: :json
56
+ patch :update, params: { id: @alpha.id, document: { title: 'Test' }, format: :json }
59
57
  end
60
58
  end
61
59
 
@@ -63,17 +61,15 @@ class DocumentsControllerTest < ActionController::TestCase
63
61
  as_bob!
64
62
 
65
63
  assert_difference 'Document.count', -1 do
66
- delete :destroy, id: @alpha.id, format: :json
64
+ delete :destroy, params: { id: @alpha.id, format: :json }
67
65
  end
68
-
69
- refute_nil assigns(:document)
70
66
  end
71
67
 
72
68
  test "destroy without access" do
73
69
  as_bob!
74
70
 
75
71
  assert_raises SimonSays::Authorizer::Denied do
76
- delete :destroy, id: @beta.id, format: :json
72
+ delete :destroy, params: { id: @beta.id, format: :json }
77
73
  end
78
74
  end
79
75
 
@@ -81,7 +77,7 @@ class DocumentsControllerTest < ActionController::TestCase
81
77
  as_jim!
82
78
 
83
79
  assert_raises ActiveRecord::RecordNotFound do
84
- delete :destroy, id: @beta.id, format: :json
80
+ delete :destroy, params: { id: @beta.id, format: :json }
85
81
  end
86
82
  end
87
83
  end
@@ -1,6 +1,7 @@
1
1
  require File.expand_path('../boot', __FILE__)
2
2
 
3
3
  require 'rails/all'
4
+ require 'responders'
4
5
 
5
6
  # Require the gems listed in Gemfile, including any gems
6
7
  # you've limited to :test, :development, or :production.
@@ -8,17 +9,6 @@ Bundler.require(*Rails.groups)
8
9
 
9
10
  module RailsApp
10
11
  class Application < Rails::Application
11
- # Settings in config/environments/* take precedence over those specified here.
12
- # Application configuration should go into files in config/initializers
13
- # -- all .rb files in that directory are automatically loaded.
14
-
15
- # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
16
- # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
17
- # config.time_zone = 'Central Time (US & Canada)'
18
-
19
- # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
20
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
21
- # config.i18n.default_locale = :de
22
12
  end
23
13
  end
24
14
 
@@ -13,8 +13,10 @@ Rails.application.configure do
13
13
  config.eager_load = true
14
14
 
15
15
  # Configure static asset server for tests with Cache-Control for performance.
16
- config.serve_static_assets = true
17
- config.static_cache_control = 'public, max-age=3600'
16
+ config.public_file_server.enabled = true
17
+ config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
18
+
19
+ config.active_support.test_order = :random
18
20
 
19
21
  # Show full error reports and disable caching.
20
22
  config.consider_all_requests_local = true
@@ -36,4 +38,8 @@ Rails.application.configure do
36
38
 
37
39
  # Raises error for missing translations
38
40
  # config.action_view.raise_on_missing_translations = true
41
+
42
+ # Use transactional fixtures
43
+ config.use_transactional_fixtures = true
44
+
39
45
  end
@@ -13,25 +13,25 @@
13
13
 
14
14
  ActiveRecord::Schema.define(version: 20141017140833) do
15
15
 
16
- create_table "admin_reports", force: true do |t|
17
- t.string "title"
16
+ create_table "admin_reports", force: :cascade do |t|
17
+ t.string "title", limit: 255
18
18
  t.datetime "created_at"
19
19
  t.datetime "updated_at"
20
20
  end
21
21
 
22
- create_table "admins", force: true do |t|
22
+ create_table "admins", force: :cascade do |t|
23
23
  t.integer "access_mask"
24
24
  t.datetime "created_at"
25
25
  t.datetime "updated_at"
26
26
  end
27
27
 
28
- create_table "documents", force: true do |t|
29
- t.string "title"
28
+ create_table "documents", force: :cascade do |t|
29
+ t.string "title", limit: 255
30
30
  t.datetime "created_at"
31
31
  t.datetime "updated_at"
32
32
  end
33
33
 
34
- create_table "memberships", force: true do |t|
34
+ create_table "memberships", force: :cascade do |t|
35
35
  t.integer "user_id"
36
36
  t.integer "document_id"
37
37
  t.integer "roles_mask", default: 0
@@ -39,7 +39,7 @@ ActiveRecord::Schema.define(version: 20141017140833) do
39
39
  t.datetime "updated_at"
40
40
  end
41
41
 
42
- create_table "users", force: true do |t|
42
+ create_table "users", force: :cascade do |t|
43
43
  t.datetime "created_at"
44
44
  t.datetime "updated_at"
45
45
  end
@@ -8,4 +8,4 @@ marketing:
8
8
  access_mask: 4
9
9
 
10
10
  all:
11
- access_mask: <%= Admin::ACCESS.size.times.map { |n| n ** 2 }.sum %>
11
+ access_mask: <%= (0..Admin::ACCESS.size - 1).map { |n| 2 ** n }.sum %>
@@ -1,7 +1,7 @@
1
1
  mb1:
2
2
  user: bob
3
3
  document: alpha
4
- roles_mask: <%= Membership::ROLES.size.times.map { |n| 2 ** n }.sum %>
4
+ roles_mask: <%= (0..Membership::ROLES.size - 1).map { |n| 2 ** n }.sum %>
5
5
 
6
6
  mb2:
7
7
  user: bob
@@ -1,197 +1,151 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class RoleableTest < ActiveSupport::TestCase
4
- setup do
5
- create_test_table :test_widgets do |t|
6
- t.integer :roles_mask, default: 0
7
- t.integer :access_mask, default: 0
8
- end
9
-
10
- @klass = Class.new(ActiveRecord::Base) do
11
- self.table_name = 'test_widgets'
12
-
13
- def self.name
14
- 'User'
15
- end
16
-
17
- include SimonSays::Roleable
18
-
19
- has_roles :read, :write
20
- end
21
-
22
- @as_klass = Class.new(ActiveRecord::Base) do
23
- self.table_name = 'test_widgets'
24
-
25
- def self.name
26
- 'Admin'
27
- end
28
-
29
- include SimonSays::Roleable
30
-
31
- has_roles :moderator, :support, :editor, as: :access
32
- end
33
-
34
- @instance = @klass.new
35
- @as_instance = @as_klass.new
36
- end
37
-
38
4
  test "adds constant" do
39
- assert_equal [:read, :write], @klass::ROLES
5
+ assert_equal [:download, :fork, :edit, :delete], Membership::ROLES
40
6
  end
41
7
 
42
8
  test "adds constant with :as option" do
43
- assert_equal [:moderator, :support, :editor], @as_klass::ACCESS
9
+ assert_equal [:support, :content, :marketing], Admin::ACCESS
44
10
  end
45
11
 
46
12
  test "adds roles method" do
47
- assert @instance.respond_to?(:roles)
48
- end
49
-
50
- test "adds roles= method" do
51
- assert @instance.respond_to?(:roles=)
13
+ assert_equal Membership::ROLES, memberships(:mb1).roles
52
14
  end
53
15
 
54
16
  test "adds reader method with :as option" do
55
- assert @as_instance.respond_to?(:access)
56
- end
57
-
58
- test "adds writer method with :as option" do
59
- assert @as_instance.respond_to?(:access=)
60
- end
61
-
62
- test 'roles returns array of symbols' do
63
- @instance.roles_mask = 3
64
-
65
- assert_equal [:read, :write], @instance.roles
66
- end
67
-
68
- test 'roles returns array of symbols with :as option' do
69
- @as_instance.access_mask = 6
70
-
71
- assert_equal [:support, :editor], @as_instance.access
17
+ assert_equal Admin::ACCESS, admins(:all).access
72
18
  end
73
19
 
74
- test "set single symbol" do
75
- @instance.roles = :read
20
+ test "set roles with multiple symbols" do
21
+ mbr = memberships(:mb2)
22
+ mbr.roles = :download, :fork
76
23
 
77
- assert_equal 1, @instance.roles_mask
24
+ assert_equal [:download, :fork], mbr.roles
78
25
  end
79
26
 
80
- test "set single symbol with :as option" do
81
- @as_instance.access = :moderator
27
+ test "set roles with multiple symbols with :as option" do
28
+ adm = admins(:support)
29
+ adm.access = :support, :marketing
82
30
 
83
- assert_equal 1, @as_instance.access_mask
31
+ assert_equal [:support, :marketing], adm.access
84
32
  end
85
33
 
86
- test "set single string" do
87
- @instance.roles = 'write'
34
+ test "set roles with single symbol" do
35
+ mbr = memberships(:mb2)
36
+ mbr.roles = :download
88
37
 
89
- assert_equal 2, @instance.roles_mask
38
+ assert_equal [:download], mbr.roles
90
39
  end
91
40
 
92
- test "set single string with :as option" do
93
- @as_instance.access = 'support'
41
+ test "set roles with single symbol with :as option" do
42
+ adm = admins(:support)
43
+ adm.access = :marketing
94
44
 
95
- assert_equal 2, @as_instance.access_mask
45
+ assert_equal [:marketing], adm.access
96
46
  end
97
47
 
98
- test "set with multi symbols" do
99
- @instance.roles = :read, :write
48
+ test "set roles with single string" do
49
+ mbr = memberships(:mb2)
50
+ mbr.roles = 'download'
100
51
 
101
- assert_equal 3, @instance.roles_mask
52
+ assert_equal [:download], mbr.roles
102
53
  end
103
54
 
104
- test "set with multi symbols with :as option" do
105
- @as_instance.access = :support, :editor
55
+ test "set roles with single string with :as option" do
56
+ adm = admins(:support)
57
+ adm.access = 'marketing'
106
58
 
107
- assert_equal 6, @as_instance.access_mask
59
+ assert_equal [:marketing], adm.access
108
60
  end
109
61
 
110
- test "set with multi strings" do
111
- @instance.roles = 'read', 'write'
62
+ test "set roles with multiple strings" do
63
+ mbr = memberships(:mb2)
64
+ mbr.roles = 'download', 'fork'
112
65
 
113
- assert_equal 3, @instance.roles_mask
66
+ assert_equal [:download, :fork], mbr.roles
114
67
  end
115
68
 
116
- test "set with multi strings with :as option" do
117
- @as_instance.access = 'support', 'editor'
69
+ test "set roles with multiples strings with :as option" do
70
+ adm = admins(:support)
71
+ adm.access = 'marketing', 'content'
118
72
 
119
- assert_equal 6, @as_instance.access_mask
73
+ assert_equal [:content, :marketing], adm.access
120
74
  end
121
75
 
122
- test 'set with array of strings' do
123
- @instance.roles = ['read', 'write']
76
+ test 'ignores unknown roles' do
77
+ mbr = memberships(:mb2)
78
+ mbr.roles = :download, :unknown
124
79
 
125
- assert_equal 3, @instance.roles_mask
80
+ assert_equal [:download], mbr.roles
126
81
  end
127
82
 
128
- test 'set with array of strings with :as option' do
129
- @as_instance.access = ['support', 'editor']
83
+ test 'handles out of order roles' do
84
+ mbr = memberships(:mb2)
85
+ mbr.roles = Membership::ROLES.reverse
130
86
 
131
- assert_equal 6, @as_instance.access_mask
132
- end
133
-
134
- test "set out order" do
135
- @as_instance.access = :editor, :moderator, :support
136
-
137
- assert_equal 7, @as_instance.access_mask
87
+ assert_equal Membership::ROLES, mbr.roles
138
88
  end
139
89
 
140
90
  test "has_roles? without any roles" do
141
- assert_equal false, @instance.has_roles?(:read)
91
+ mbr = memberships(:mb1)
92
+ mbr.roles = nil
93
+
94
+ assert_equal false, mbr.has_roles?(:download)
142
95
  end
143
96
 
144
97
  test "has_roles? with one role" do
145
- @instance.roles = :read
98
+ mbr = memberships(:mb1)
99
+ mbr.roles = :download
146
100
 
147
- assert_equal true, @instance.has_roles?(:read)
101
+ assert_equal true, mbr.has_roles?(:download)
148
102
  end
149
103
 
150
104
  test "has_roles? with multiple role" do
151
- @instance.roles = :read, :write
105
+ mbr = memberships(:mb1)
106
+ mbr.roles = :download, :fork, :edit
152
107
 
153
- assert_equal true, @instance.has_roles?(:read, :write)
108
+ assert_equal true, mbr.has_roles?(:download, :fork, :edit)
154
109
  end
155
110
 
156
111
  test "has_access? without any roles" do
157
- assert_equal false, @as_instance.has_access?(:support)
112
+ adm = admins(:support)
113
+ adm.access = nil
114
+
115
+ assert_equal false, adm.has_access?(:support)
158
116
  end
159
117
 
160
118
  test "has_access? with one role" do
161
- @as_instance.access = :editor
119
+ adm = admins(:support)
120
+ adm.access = :marketing
162
121
 
163
- assert_equal true, @as_instance.has_access?(:editor)
122
+ assert_equal true, adm.has_access?(:marketing)
164
123
  end
165
124
 
166
125
  test "has_access? with multiple role" do
167
- @as_instance.access = :moderator, :support
126
+ adm = admins(:support)
127
+ adm.access = :support, :content, :marketing
168
128
 
169
- assert_equal true, @as_instance.has_access?(:moderator, :support)
129
+ assert_equal true, adm.has_access?(:support, :content, :marketing)
170
130
  end
171
131
 
172
- test "named scope" do
173
- @klass.create roles: :read
174
- @klass.create roles: :write
175
-
176
- assert_equal [1, 1], [
177
- @klass.with_roles(:read).count,
178
- @klass.with_roles(:write).count
132
+ test "named scope with_roles" do
133
+ assert_equal [2, 1], [
134
+ Membership.with_roles(:download).count,
135
+ Membership.with_roles(:delete).count
179
136
  ]
180
137
  end
181
138
 
182
- test "named scope with :as option" do
183
- @as_klass.create access: :moderator
184
- @as_klass.create access: [:support, :editor]
185
-
186
- assert_equal [1, 1, 1], [
187
- @as_klass.with_access(:moderator).count,
188
- @as_klass.with_access(:editor).count,
189
- @as_klass.with_access(:support).count,
139
+ test "named scope with_access" do
140
+ assert_equal [2, 2, 2], [
141
+ Admin.with_access(:marketing).count,
142
+ Admin.with_access(:content).count,
143
+ Admin.with_access(:support).count
190
144
  ]
191
145
  end
192
146
 
193
- test "User is added to registry" do
194
- assert_includes SimonSays::Roleable.registry, :user
147
+ test "Membership is added to registry" do
148
+ assert_includes SimonSays::Roleable.registry, :membership
195
149
  end
196
150
 
197
151
  test "Admin is added to registry" do
data/test/test_helper.rb CHANGED
@@ -1,3 +1,8 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'mocha/mini_test'
5
+
1
6
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
7
  require 'simon_says' # HELLO SIMON
3
8
 
@@ -26,7 +31,7 @@ ActiveRecord::Migrator.migrate(File.expand_path("../rails_app/db/migrate/", __FI
26
31
  class ActiveSupport::TestCase
27
32
  include ActiveRecord::TestFixtures
28
33
 
29
- fixtures :users, :admins, :documents, :'admin/reports'
34
+ fixtures :all
30
35
 
31
36
  def create_test_table(name, &block)
32
37
  with_migration { |m| m.create_table name, &block }
metadata CHANGED
@@ -1,43 +1,51 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simon_says
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.27b6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Coyne
8
+ - Cameron Craig
9
+ - SimplyBuilt
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2014-11-12 00:00:00.000000000 Z
13
+ date: 2016-07-21 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: activesupport
15
17
  requirement: !ruby/object:Gem::Requirement
16
18
  requirements:
17
- - - "~>"
19
+ - - ">="
18
20
  - !ruby/object:Gem::Version
19
21
  version: '4.0'
22
+ - - "<"
23
+ - !ruby/object:Gem::Version
24
+ version: '5.1'
20
25
  type: :runtime
21
26
  prerelease: false
22
27
  version_requirements: !ruby/object:Gem::Requirement
23
28
  requirements:
24
- - - "~>"
29
+ - - ">="
25
30
  - !ruby/object:Gem::Version
26
31
  version: '4.0'
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '5.1'
27
35
  - !ruby/object:Gem::Dependency
28
36
  name: bundler
29
37
  requirement: !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - "~>"
32
40
  - !ruby/object:Gem::Version
33
- version: '1.7'
41
+ version: '1.9'
34
42
  type: :development
35
43
  prerelease: false
36
44
  version_requirements: !ruby/object:Gem::Requirement
37
45
  requirements:
38
46
  - - "~>"
39
47
  - !ruby/object:Gem::Version
40
- version: '1.7'
48
+ version: '1.9'
41
49
  - !ruby/object:Gem::Dependency
42
50
  name: rake
43
51
  requirement: !ruby/object:Gem::Requirement
@@ -54,18 +62,38 @@ dependencies:
54
62
  version: '10.0'
55
63
  - !ruby/object:Gem::Dependency
56
64
  name: rails
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '4.0'
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '5.1'
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '4.0'
80
+ - - "<"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: responders
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - "~>"
60
88
  - !ruby/object:Gem::Version
61
- version: '4.1'
89
+ version: '2.0'
62
90
  type: :development
63
91
  prerelease: false
64
92
  version_requirements: !ruby/object:Gem::Requirement
65
93
  requirements:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
- version: '4.1'
96
+ version: '2.0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: mocha
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -105,11 +133,7 @@ files:
105
133
  - simon_says.gemspec
106
134
  - test/controllers/admin/reports_controller_test.rb
107
135
  - test/controllers/documents_controller_test.rb
108
- - test/models/admin_test.rb
109
- - test/models/membership_test.rb
110
136
  - test/rails_app/.gitignore
111
- - test/rails_app/Gemfile
112
- - test/rails_app/Gemfile.lock
113
137
  - test/rails_app/README.rdoc
114
138
  - test/rails_app/Rakefile
115
139
  - test/rails_app/app/assets/images/.keep
@@ -204,23 +228,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
204
228
  version: '0'
205
229
  required_rubygems_version: !ruby/object:Gem::Requirement
206
230
  requirements:
207
- - - ">"
231
+ - - ">="
208
232
  - !ruby/object:Gem::Version
209
- version: 1.3.1
233
+ version: '0'
210
234
  requirements: []
211
235
  rubyforge_project:
212
- rubygems_version: 2.4.2
236
+ rubygems_version: 2.4.5.1
213
237
  signing_key:
214
238
  specification_version: 4
215
239
  summary: Light-weight, declarative authorization and access control for Rails
216
240
  test_files:
217
241
  - test/controllers/admin/reports_controller_test.rb
218
242
  - test/controllers/documents_controller_test.rb
219
- - test/models/admin_test.rb
220
- - test/models/membership_test.rb
221
243
  - test/rails_app/.gitignore
222
- - test/rails_app/Gemfile
223
- - test/rails_app/Gemfile.lock
224
244
  - test/rails_app/README.rdoc
225
245
  - test/rails_app/Rakefile
226
246
  - test/rails_app/app/assets/images/.keep
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class AdminModelTest < ActiveSupport::TestCase
4
- test "Admin is in registry" do
5
- assert_includes SimonSays::Roleable.registry, :admin
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class MembershipModelTest < ActiveSupport::TestCase
4
- test "Membership is in registry" do
5
- assert_includes SimonSays::Roleable.registry, :membership
6
- end
7
- end
@@ -1,40 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
-
4
- # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
5
- gem 'rails', '4.1.6'
6
- # Use sqlite3 as the database for Active Record
7
- gem 'sqlite3'
8
- # Use SCSS for stylesheets
9
- gem 'sass-rails', '~> 4.0.3'
10
- # Use Uglifier as compressor for JavaScript assets
11
- gem 'uglifier', '>= 1.3.0'
12
- # Use CoffeeScript for .js.coffee assets and views
13
- gem 'coffee-rails', '~> 4.0.0'
14
- # See https://github.com/sstephenson/execjs#readme for more supported runtimes
15
- # gem 'therubyracer', platforms: :ruby
16
-
17
- # Use jquery as the JavaScript library
18
- gem 'jquery-rails'
19
- # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
20
- gem 'turbolinks'
21
- # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
22
- gem 'jbuilder', '~> 2.0'
23
- # bundle exec rake doc:rails generates the API under doc/api.
24
- gem 'sdoc', '~> 0.4.0', group: :doc
25
-
26
- # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
27
- gem 'spring', group: :development
28
-
29
- # Use ActiveModel has_secure_password
30
- # gem 'bcrypt', '~> 3.1.7'
31
-
32
- # Use unicorn as the app server
33
- # gem 'unicorn'
34
-
35
- # Use Capistrano for deployment
36
- # gem 'capistrano-rails', group: :development
37
-
38
- # Use debugger
39
- # gem 'debugger', group: [:development, :test]
40
-
@@ -1,120 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- actionmailer (4.1.6)
5
- actionpack (= 4.1.6)
6
- actionview (= 4.1.6)
7
- mail (~> 2.5, >= 2.5.4)
8
- actionpack (4.1.6)
9
- actionview (= 4.1.6)
10
- activesupport (= 4.1.6)
11
- rack (~> 1.5.2)
12
- rack-test (~> 0.6.2)
13
- actionview (4.1.6)
14
- activesupport (= 4.1.6)
15
- builder (~> 3.1)
16
- erubis (~> 2.7.0)
17
- activemodel (4.1.6)
18
- activesupport (= 4.1.6)
19
- builder (~> 3.1)
20
- activerecord (4.1.6)
21
- activemodel (= 4.1.6)
22
- activesupport (= 4.1.6)
23
- arel (~> 5.0.0)
24
- activesupport (4.1.6)
25
- i18n (~> 0.6, >= 0.6.9)
26
- json (~> 1.7, >= 1.7.7)
27
- minitest (~> 5.1)
28
- thread_safe (~> 0.1)
29
- tzinfo (~> 1.1)
30
- arel (5.0.1.20140414130214)
31
- builder (3.2.2)
32
- coffee-rails (4.0.1)
33
- coffee-script (>= 2.2.0)
34
- railties (>= 4.0.0, < 5.0)
35
- coffee-script (2.3.0)
36
- coffee-script-source
37
- execjs
38
- coffee-script-source (1.8.0)
39
- erubis (2.7.0)
40
- execjs (2.2.2)
41
- hike (1.2.3)
42
- i18n (0.6.11)
43
- jbuilder (2.2.2)
44
- activesupport (>= 3.0.0, < 5)
45
- multi_json (~> 1.2)
46
- jquery-rails (3.1.2)
47
- railties (>= 3.0, < 5.0)
48
- thor (>= 0.14, < 2.0)
49
- json (1.8.1)
50
- mail (2.6.1)
51
- mime-types (>= 1.16, < 3)
52
- mime-types (2.4.2)
53
- minitest (5.4.2)
54
- multi_json (1.10.1)
55
- rack (1.5.2)
56
- rack-test (0.6.2)
57
- rack (>= 1.0)
58
- rails (4.1.6)
59
- actionmailer (= 4.1.6)
60
- actionpack (= 4.1.6)
61
- actionview (= 4.1.6)
62
- activemodel (= 4.1.6)
63
- activerecord (= 4.1.6)
64
- activesupport (= 4.1.6)
65
- bundler (>= 1.3.0, < 2.0)
66
- railties (= 4.1.6)
67
- sprockets-rails (~> 2.0)
68
- railties (4.1.6)
69
- actionpack (= 4.1.6)
70
- activesupport (= 4.1.6)
71
- rake (>= 0.8.7)
72
- thor (>= 0.18.1, < 2.0)
73
- rake (10.3.2)
74
- rdoc (4.1.2)
75
- json (~> 1.4)
76
- sass (3.2.19)
77
- sass-rails (4.0.3)
78
- railties (>= 4.0.0, < 5.0)
79
- sass (~> 3.2.0)
80
- sprockets (~> 2.8, <= 2.11.0)
81
- sprockets-rails (~> 2.0)
82
- sdoc (0.4.1)
83
- json (~> 1.7, >= 1.7.7)
84
- rdoc (~> 4.0)
85
- spring (1.1.3)
86
- sprockets (2.11.0)
87
- hike (~> 1.2)
88
- multi_json (~> 1.0)
89
- rack (~> 1.0)
90
- tilt (~> 1.1, != 1.3.0)
91
- sprockets-rails (2.2.0)
92
- actionpack (>= 3.0)
93
- activesupport (>= 3.0)
94
- sprockets (>= 2.8, < 4.0)
95
- sqlite3 (1.3.9)
96
- thor (0.19.1)
97
- thread_safe (0.3.4)
98
- tilt (1.4.1)
99
- turbolinks (2.4.0)
100
- coffee-rails
101
- tzinfo (1.2.2)
102
- thread_safe (~> 0.1)
103
- uglifier (2.5.3)
104
- execjs (>= 0.3.0)
105
- json (>= 1.8.0)
106
-
107
- PLATFORMS
108
- ruby
109
-
110
- DEPENDENCIES
111
- coffee-rails (~> 4.0.0)
112
- jbuilder (~> 2.0)
113
- jquery-rails
114
- rails (= 4.1.6)
115
- sass-rails (~> 4.0.3)
116
- sdoc (~> 0.4.0)
117
- spring
118
- sqlite3
119
- turbolinks
120
- uglifier (>= 1.3.0)