average_roles 0.0.1

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.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/config/initializers/average_roles.rb +8 -0
  5. data/lib/average_roles.rb +25 -0
  6. data/lib/average_roles/engine.rb +4 -0
  7. data/lib/average_roles/models/concerns/role.rb +22 -0
  8. data/lib/average_roles/models/concerns/user.rb +130 -0
  9. data/lib/average_roles/version.rb +3 -0
  10. data/lib/tasks/average_roles_tasks.rake +4 -0
  11. data/test/average_roles_test.rb +7 -0
  12. data/test/dummy/README.rdoc +28 -0
  13. data/test/dummy/Rakefile +6 -0
  14. data/test/dummy/app/assets/javascripts/application.js +13 -0
  15. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  16. data/test/dummy/app/controllers/application_controller.rb +5 -0
  17. data/test/dummy/app/helpers/application_helper.rb +2 -0
  18. data/test/dummy/app/models/role.rb +3 -0
  19. data/test/dummy/app/models/user.rb +3 -0
  20. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  21. data/test/dummy/bin/bundle +3 -0
  22. data/test/dummy/bin/rails +4 -0
  23. data/test/dummy/bin/rake +4 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +24 -0
  26. data/test/dummy/config/boot.rb +5 -0
  27. data/test/dummy/config/database.yml +89 -0
  28. data/test/dummy/config/database.yml.travis +4 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +37 -0
  31. data/test/dummy/config/environments/production.rb +78 -0
  32. data/test/dummy/config/environments/test.rb +39 -0
  33. data/test/dummy/config/initializers/assets.rb +8 -0
  34. data/test/dummy/config/initializers/average_roles.rb +8 -0
  35. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  37. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/test/dummy/config/initializers/inflections.rb +16 -0
  39. data/test/dummy/config/initializers/mime_types.rb +4 -0
  40. data/test/dummy/config/initializers/session_store.rb +3 -0
  41. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  42. data/test/dummy/config/locales/en.yml +23 -0
  43. data/test/dummy/config/routes.rb +56 -0
  44. data/test/dummy/config/secrets.yml +22 -0
  45. data/test/dummy/db/migrate/20140929035502_create_users.rb +9 -0
  46. data/test/dummy/db/migrate/20140929035606_create_roles.rb +13 -0
  47. data/test/dummy/db/migrate/20140929070323_create_roles_users.rb +8 -0
  48. data/test/dummy/db/schema.rb +34 -0
  49. data/test/dummy/log/development.log +15 -0
  50. data/test/dummy/log/test.log +18907 -0
  51. data/test/dummy/public/404.html +67 -0
  52. data/test/dummy/public/422.html +67 -0
  53. data/test/dummy/public/500.html +66 -0
  54. data/test/dummy/public/favicon.ico +0 -0
  55. data/test/dummy/test/fixtures/roles.yml +37 -0
  56. data/test/dummy/test/fixtures/users.yml +16 -0
  57. data/test/dummy/test/models/role_test.rb +81 -0
  58. data/test/dummy/test/models/user_test.rb +184 -0
  59. data/test/integration/navigation_test.rb +10 -0
  60. data/test/test_helper.rb +46 -0
  61. metadata +196 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d77d5c88f35e3f89d9a1ba1a848f2dd0f7b7db62
4
+ data.tar.gz: 8cec02983a17eeb5e44eae869bae4a8956636e5a
5
+ SHA512:
6
+ metadata.gz: 42ac6be770264d131a87446e36c2ebae9e452183975f99fd49d46da8c6209568c04916abc0a4afd1036591edc0d69dea56d2a199cc8a6fcfa71eddc5797cdd02
7
+ data.tar.gz: cb908a9f0ea7de1d5da4b592ddbddb6f016cbddae8bb8d92160938145245096005c59d28c4be3bc11d95cc9c41e56227828470b8b9abc37781bc15d7d603743c
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AverageRoles'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,8 @@
1
+ # Uncomment and change the defaults below to use different models
2
+ # and to set a super user role which if someone has, will return all checks
3
+ # for roles true.
4
+ # AverageRoles.configure do |config|
5
+ # config.user_model = 'User'
6
+ # config.role_model = 'Role'
7
+ # config.super_user = nil # or identifier as symbol
8
+ # end
@@ -0,0 +1,25 @@
1
+ require "average_roles/engine"
2
+ require "average_roles/models/concerns/role"
3
+ require "average_roles/models/concerns/user"
4
+
5
+ module AverageRoles
6
+
7
+ def self.configuration
8
+ @configuration ||= Configuration.new
9
+ end
10
+
11
+ def self.configure
12
+ yield(configuration) if block_given?
13
+ end
14
+
15
+ class Configuration
16
+ attr_accessor :user_model, :role_model, :super_user
17
+
18
+ def initialize
19
+ @user_model = 'User'
20
+ @role_model = 'Role'
21
+ @super_user = nil
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,4 @@
1
+ module AverageRoles
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_support/concern'
2
+
3
+ module AverageRoles::RoleConcern
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include PostgresTree::ActiveRecordConcern
9
+
10
+ has_and_belongs_to_many AverageRoles.configuration.user_model.downcase.pluralize.to_sym
11
+
12
+ # Associations for tree
13
+ belongs_to :parent, class_name: AverageRoles.configuration.role_model
14
+ has_many :children, class_name: AverageRoles.configuration.role_model, foreign_key: 'parent_id'
15
+
16
+ # Validations
17
+ validates :name, presence: true, uniqueness: true
18
+ validates :identifier, presence: true, uniqueness: true
19
+ validates :description, presence: true
20
+ end
21
+
22
+ end
@@ -0,0 +1,130 @@
1
+ require 'active_support/concern'
2
+
3
+ module AverageRoles::UserConcern
4
+
5
+ # TODO: Add a super user identifier that always returns true when checking if has role. This needs
6
+ # to be a configuration option.
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ has_and_belongs_to_many AverageRoles.configuration.role_model.downcase.pluralize.to_sym
12
+
13
+ # Get roles by type
14
+ define_method AverageRoles.configuration.role_model.downcase.pluralize + '_as' do |type|
15
+ average_roles_check_type type
16
+ case type
17
+ when :identifiers
18
+ roles = []
19
+ self.send(AverageRoles.configuration.role_model.downcase.pluralize.to_sym).where(active: true).load.each do |role|
20
+ roles << role.identifier.to_sym
21
+ end
22
+ roles
23
+ when :ids
24
+ return self.send (AverageRoles.configuration.role_model.downcase + '_ids').to_sym
25
+ when :objects
26
+ return self.send AverageRoles.configuration.role_model.downcase.pluralize.to_sym
27
+ end
28
+ end
29
+
30
+ # Check if has role
31
+ define_method 'has_' + AverageRoles.configuration.role_model.downcase + '?' do |role|
32
+ return true if average_roles_has_super_user?
33
+ if role.is_a? Symbol
34
+ self.roles_as(:identifiers).include? role
35
+ elsif role.is_a? Numeric
36
+ self.send(AverageRoles.configuration.role_model.downcase + '_ids').include? role
37
+ elsif role.is_a? AverageRoles.configuration.role_model.constantize.new.class
38
+ self.send(AverageRoles.configuration.role_model.downcase + '_ids').include? role.id
39
+ else
40
+ raise ArgumentError, "Role must be a symbol, number or object of same type"
41
+ end
42
+ end
43
+
44
+ # Check if has all roles
45
+ define_method 'has_' + AverageRoles.configuration.role_model.downcase.pluralize + '?' do |roles|
46
+ return true if average_roles_has_super_user?
47
+ roles.each do |role|
48
+ return false unless self.has_role? role
49
+ end
50
+ true
51
+ end
52
+
53
+ # Check if has at least one role
54
+ define_method 'has_at_least_one_' + AverageRoles.configuration.role_model.downcase + '?' do |roles|
55
+ return true if average_roles_has_super_user?
56
+ roles.each do |role|
57
+ return true if self.has_role? role
58
+ end
59
+ false
60
+ end
61
+
62
+ # Get roles and descendent roles by type
63
+ define_method AverageRoles.configuration.role_model.downcase.pluralize + '_and_descendents_as' do |type|
64
+ average_roles_check_type type
65
+ roles = []
66
+ self.send(AverageRoles.configuration.role_model.pluralize.downcase.to_sym).where(active: true).load.each do |role|
67
+ role.self_and_descendents.each do |r|
68
+ case type
69
+ when :identifiers
70
+ roles << r.identifier.to_sym
71
+ when :ids
72
+ roles << r.id
73
+ when :objects
74
+ roles << r
75
+ end
76
+ end
77
+ end
78
+ roles.uniq
79
+ end
80
+
81
+ # Check if object has role or a ancestors ancestor role
82
+ define_method 'has_' + AverageRoles.configuration.role_model.downcase + '_by_descendents?' do |role|
83
+ return true if average_roles_has_super_user?
84
+ if role.is_a? Symbol
85
+ self.roles_and_descendents_as(:identifiers).include? role
86
+ elsif role.is_a? Numeric
87
+ self.roles_and_descendents_as(:ids).include? role
88
+ elsif role.is_a? AverageRoles.configuration.role_model.constantize.new.class
89
+ self.roles_and_descendents_as(:ids).include? role.id # Use ids to save some memory
90
+ else
91
+ raise ArgumentError, "Role must be a symbol, number or object of same type"
92
+ end
93
+ end
94
+
95
+ # Check if has all roles or at least a ancestor of each role
96
+ define_method 'has_' + AverageRoles.configuration.role_model.downcase.pluralize + '_by_descendents?' do |roles|
97
+ return true if average_roles_has_super_user?
98
+ roles.each do |role|
99
+ return false unless self.has_role_by_descendents? role
100
+ end
101
+ true
102
+ end
103
+
104
+ # Check if has at least one role or at least one ancestor of one role
105
+ define_method 'has_at_least_one_' + AverageRoles.configuration.role_model.downcase + '_by_descendents?' do |roles|
106
+ return true if average_roles_has_super_user?
107
+ roles.each do |role|
108
+ return true if self.has_role_by_descendents? role
109
+ end
110
+ false
111
+ end
112
+
113
+ end
114
+
115
+ private
116
+
117
+ # check type
118
+ def average_roles_check_type(type)
119
+ if not [:identifiers, :ids, :objects].include? type
120
+ raise ArgumentError, "Type must be :identifiers, :ids or :objects"
121
+ end
122
+ end
123
+
124
+ # check if the user has the super user role
125
+ def average_roles_has_super_user?
126
+ return self.roles_as(:identifiers).include?(AverageRoles.configuration.super_user) unless AverageRoles.configuration.super_user == nil
127
+ return false
128
+ end
129
+
130
+ end
@@ -0,0 +1,3 @@
1
+ module AverageRoles
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :average_roles do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class AverageRolesTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, AverageRoles
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class Role < ActiveRecord::Base
2
+ include AverageRoles::RoleConcern
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ include AverageRoles::UserConcern
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "average_roles"
7
+ require "postgres_tree"
8
+
9
+ module Dummy
10
+ 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
+ end
23
+ end
24
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,89 @@
1
+ # PostgreSQL. Versions 8.2 and up are supported.
2
+ #
3
+ # Install the pg driver:
4
+ # gem install pg
5
+ # On OS X with Homebrew:
6
+ # gem install pg -- --with-pg-config=/usr/local/bin/pg_config
7
+ # On OS X with MacPorts:
8
+ # gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
9
+ # On Windows:
10
+ # gem install pg
11
+ # Choose the win32 build.
12
+ # Install PostgreSQL and put its /bin directory on your path.
13
+ #
14
+ # Configure Using Gemfile
15
+ # gem 'pg'
16
+ #
17
+ default: &default
18
+ adapter: postgresql
19
+ encoding: unicode
20
+ # For details on connection pooling, see rails configuration guide
21
+ # http://guides.rubyonrails.org/configuring.html#database-pooling
22
+ pool: 5
23
+
24
+ development:
25
+ <<: *default
26
+ database: average_roles_development
27
+ username: postgres
28
+ password: postgres
29
+
30
+ # The specified database role being used to connect to postgres.
31
+ # To create additional roles in postgres see `$ createuser --help`.
32
+ # When left blank, postgres will use the default role. This is
33
+ # the same name as the operating system user that initialized the database.
34
+ #username: dummy
35
+
36
+ # The password associated with the postgres role (username).
37
+ #password:
38
+
39
+ # Connect on a TCP socket. Omitted by default since the client uses a
40
+ # domain socket that doesn't need configuration. Windows does not have
41
+ # domain sockets, so uncomment these lines.
42
+ #host: localhost
43
+
44
+ # The TCP port the server listens on. Defaults to 5432.
45
+ # If your server runs on a different port number, change accordingly.
46
+ #port: 5432
47
+
48
+ # Schema search path. The server defaults to $user,public
49
+ #schema_search_path: myapp,sharedapp,public
50
+
51
+ # Minimum log levels, in increasing order:
52
+ # debug5, debug4, debug3, debug2, debug1,
53
+ # log, notice, warning, error, fatal, and panic
54
+ # Defaults to warning.
55
+ #min_messages: notice
56
+
57
+ # Warning: The database defined as "test" will be erased and
58
+ # re-generated from your development database when you run "rake".
59
+ # Do not set this db to the same as development or production.
60
+ test:
61
+ <<: *default
62
+ database: average_roles_test
63
+ username: postgres
64
+ password: postgres
65
+
66
+ # As with config/secrets.yml, you never want to store sensitive information,
67
+ # like your database password, in your source code. If your source code is
68
+ # ever seen by anyone, they now have access to your database.
69
+ #
70
+ # Instead, provide the password as a unix environment variable when you boot
71
+ # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
72
+ # for a full rundown on how to provide these environment variables in a
73
+ # production deployment.
74
+ #
75
+ # On Heroku and other platform providers, you may have a full connection URL
76
+ # available as an environment variable. For example:
77
+ #
78
+ # DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
79
+ #
80
+ # You can use this database configuration with:
81
+ #
82
+ # production:
83
+ # url: <%= ENV['DATABASE_URL'] %>
84
+ #
85
+ production:
86
+ <<: *default
87
+ database: average_roles_production
88
+ username: postgres
89
+ password: <%= ENV['DUMMY_DATABASE_PASSWORD'] %>