rhino_project_organizations 0.20.0.beta.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +28 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/config/rhino_organizations_manifest.js +0 -0
  5. data/app/controllers/rhino/users_role_invite_controller.rb +14 -0
  6. data/app/controllers/rhino_organizations/concerns/create_organization.rb +15 -0
  7. data/app/models/rhino/organization.rb +33 -0
  8. data/app/models/rhino/role.rb +11 -0
  9. data/app/models/rhino/users_role.rb +35 -0
  10. data/app/models/rhino/users_role_invite.rb +25 -0
  11. data/app/overrides/devise/invitations_controller_override.rb +50 -0
  12. data/app/overrides/models/rhino/account_override.rb +7 -0
  13. data/app/overrides/models/rhino/users_role_override.rb +7 -0
  14. data/app/policies/rhino/organization_policy.rb +17 -0
  15. data/app/policies/rhino/users_role_policy.rb +33 -0
  16. data/app/views/devise/mailer/invitation_instructions.html.erb +11 -0
  17. data/app/views/devise/mailer/invitation_instructions.text.erb +11 -0
  18. data/config/routes.rb +2 -0
  19. data/db/migrate/20201129221747_create_organizations.rb +23 -0
  20. data/db/migrate/20211202195016_add_devise_invitable_to_users.rb +12 -0
  21. data/db/migrate/20211207212517_create_users_role_invite.rb +11 -0
  22. data/lib/generators/rhino_organizations/install/install_generator.rb +62 -0
  23. data/lib/generators/rhino_organizations/install/templates/admin/users_roles.rb +5 -0
  24. data/lib/generators/rhino_organizations/install/templates/models/organization.rb +8 -0
  25. data/lib/generators/rhino_organizations/install/templates/models/role.rb +4 -0
  26. data/lib/generators/rhino_organizations/install/templates/models/users_role.rb +7 -0
  27. data/lib/generators/rhino_organizations/install/templates/models/users_role_invite.rb +6 -0
  28. data/lib/rhino_organizations/engine.rb +35 -0
  29. data/lib/rhino_organizations/test_case/organization_controller.rb +38 -0
  30. data/lib/rhino_organizations/test_case/organization_controller_policy.rb +84 -0
  31. data/lib/rhino_organizations/test_case.rb +10 -0
  32. data/lib/rhino_organizations/version.rb +17 -0
  33. data/lib/rhino_project_organizations.rb +6 -0
  34. data/lib/tasks/rhino_organizations.rake +15 -0
  35. metadata +110 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe0d222969d9e996100fa97f1bbc9e108bea08761393b1dc499fe3013ae3a069
4
+ data.tar.gz: 2a1c60f12b2475dcd4c6ce0ee43812e426016f80b62c63c053e706b2cb608012
5
+ SHA512:
6
+ metadata.gz: a5c6424c003e72e2c699185866879070bd32b944062ce06d68055a84f232cb3dd868ca95dc922708c04676535a208871551cb77f4f0a22e154141df51a18674a
7
+ data.tar.gz: a5fb9c081d8451d0c7e1afa4b1c17fb4f6aac64298797853f55e4cdc8b8eaf7945c2218203a81982e2030267d24ee2261ed8f4bffe52c335f31f486de5bfc634
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # RhinoOrganizations
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rhino_organizations'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install rhino_organizations
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
+ task :package
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'RhinoOrganizations'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
20
+ load 'rails/tasks/engine.rake'
21
+
22
+ load 'rails/tasks/statistics.rake'
23
+
24
+ require 'bundler/gem_tasks'
25
+
26
+ require 'rake/testtask'
27
+
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = false
32
+ end
33
+
34
+ task default: :test
File without changes
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class UsersRoleInviteController < CrudController
5
+ def create
6
+ @model = authorize klass.new(permit_and_transform(klass))
7
+ user = ::User.find_by(email: params[:email])
8
+ ::User.invite!(email: params[:email]) if user.nil?
9
+ @model.save!
10
+
11
+ permit_and_render
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RhinoOrganizations
4
+ module Concerns
5
+ module CreateOrganization
6
+ extend ActiveSupport::Concern
7
+
8
+ def create_organization(resource)
9
+ org_name = params.fetch(:organization, nil) || resource.name || resource.email
10
+ org = Organization.create!(name: "#{org_name} Organization")
11
+ UsersRole.create!(organization: org, user: resource, role: Role.find_or_create_by!(name: 'admin'))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class Organization < ApplicationRecord
5
+ self.abstract_class = true
6
+
7
+ rhino_policy :organization
8
+ rhino_properties_write only: :name
9
+
10
+ after_create_commit :track_account_created
11
+ after_destroy_commit :track_account_deleted
12
+
13
+ def self.roles_for_auth(auth_owner, record = nil)
14
+ return {} unless auth_owner
15
+
16
+ # FIXME: - hard code user?
17
+ users_roles = ::UsersRole.where(user: auth_owner).joins(:organization, :role).includes(:organization, :role)
18
+ users_roles = users_roles.where(organization_id: record.base_owner_ids) if record.present? && record.respond_to?(:base_owner_ids)
19
+
20
+ # A list of roles as hash keys with an array of base_owners for each
21
+ users_roles.group_by { |ur| ur.role.name }.transform_values { |ur_array| ur_array.map(&:organization) }
22
+ end
23
+
24
+ private
25
+ def track_account_created
26
+ Rhino::SegmentHelper.track_account("Account Created", self)
27
+ end
28
+
29
+ def track_account_deleted
30
+ Rhino::SegmentHelper.track_account("Account Deleted", self)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class Role < ApplicationRecord
5
+ self.abstract_class = true
6
+
7
+ validates :name, presence: true, uniqueness: true, format: { with: /\A[a-zA-Z]+\z/, message: "alpha characters only" }
8
+
9
+ rhino_owner_global
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class UsersRole < ApplicationRecord
5
+ self.abstract_class = true
6
+
7
+ rhino_properties_update only: [:role]
8
+
9
+ validate :ensure_at_least_one_admin, on: %i[update destroy], unless: :destroyed_by_association
10
+
11
+ after_create_commit :track_account_added_user
12
+ after_destroy_commit :track_account_removed_user
13
+
14
+ before_destroy do
15
+ raise ActiveRecord::RecordInvalid, self if invalid?(:destroy)
16
+ end
17
+
18
+ private
19
+ def ensure_at_least_one_admin
20
+ admins = organization.users_roles.joins(:role).where({ role: { name: "admin" } })
21
+ user_role_id_being_updated = id
22
+ return unless admins.count == 1 && admins.first.id == user_role_id_being_updated
23
+
24
+ errors.add(:role, "Must have at least one user as admin")
25
+ end
26
+
27
+ def track_account_added_user
28
+ Rhino::SegmentHelper.track_account("Account Added User", organization, self)
29
+ end
30
+
31
+ def track_account_removed_user
32
+ Rhino::SegmentHelper.track_account("Account Removed User", organization, self)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class UsersRoleInvite < ApplicationRecord
5
+ self.abstract_class = true
6
+
7
+ rhino_policy :users_role
8
+ # FIXME: Only necessary because this module is in tree NUB-910
9
+ rhino_owner :organization if Rhino.resources.include?("Organization")
10
+ rhino_references %i[role organization]
11
+
12
+ rhino_controller :users_role_invite
13
+
14
+ after_create do
15
+ ::UsersRole.create!(user: ::User.find_by(email:), organization:, role:)
16
+ end
17
+
18
+ after_create_commit :track_invite
19
+
20
+ private
21
+ def track_invite
22
+ Rhino::SegmentHelper.track_invite(self)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Devise::InvitationsController
4
+ prepend_before_action :require_no_authentication, only: %i[update]
5
+ before_action :configure_permitted_parameters
6
+
7
+ # PUT /resource/invitation
8
+ def update
9
+ self.resource = accept_resource
10
+ invitation_accepted = resource.errors.empty?
11
+
12
+ if invitation_accepted
13
+ actions_for_insecure_sign_in
14
+ render json: { data: resource.token_validation_response }
15
+ else
16
+ render status: :unprocessable_entity, json: { status: :error, errors: resource.errors }
17
+ end
18
+ end
19
+
20
+ protected
21
+ def actions_for_insecure_sign_in
22
+ return unless resource.class.allow_insecure_sign_in_after_accept
23
+
24
+ resource.after_database_authentication
25
+ set_token
26
+ set_cookie
27
+ end
28
+
29
+ def set_token
30
+ @token = resource.create_token
31
+ resource.skip_confirmation!
32
+ sign_in(:user, resource, store: false, bypass: false)
33
+ resource.save!
34
+ end
35
+
36
+ def set_cookie
37
+ return unless DeviseTokenAuth.cookie_enabled
38
+
39
+ auth_header = resource.build_auth_header(@token.token, @token.client)
40
+ cookies[DeviseTokenAuth.cookie_name] = DeviseTokenAuth.cookie_attributes.merge(value: auth_header.to_json)
41
+ end
42
+
43
+ def accept_resource
44
+ resource_class.accept_invitation!(@update_resource_params)
45
+ end
46
+
47
+ def configure_permitted_parameters
48
+ @update_resource_params = params.permit(%i[invitation_token password password_confirmation])
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Rhino::Account
4
+ rhino_properties_read only: %i[id name nickname email image users_roles]
5
+
6
+ rhino_references [users_roles: %i[organization role]]
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Rhino::UsersRole
4
+ rhino_policy :users_role
5
+ rhino_owner :organization
6
+ rhino_references %i[user role organization]
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class OrganizationPolicy < ::Rhino::ViewerPolicy
5
+ def update?
6
+ # Must be an admin to update
7
+ is_admin = Rhino.base_owner.roles_for_auth(auth_owner).any? { |k, v| k == "admin" && v.include?(record) }
8
+ authorize_action(is_admin)
9
+ end
10
+
11
+ class Scope < ::Rhino::ViewerPolicy::Scope
12
+ def resolve
13
+ super.joins(scope.joins_for_auth_owner).where("#{Rhino.auth_owner.table_name}.#{Rhino.auth_owner.primary_key}": auth_owner&.id)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class UsersRolePolicy < ::Rhino::ViewerPolicy
5
+ def show?
6
+ authorize_action(true) if admin? || record.user == auth_owner
7
+ end
8
+
9
+ def create?
10
+ authorize_action admin?
11
+ end
12
+
13
+ def update?
14
+ authorize_action admin?
15
+ end
16
+
17
+ def destroy?
18
+ authorize_action admin?
19
+ end
20
+
21
+ private
22
+ def admin?
23
+ Rhino.base_owner.roles_for_auth(auth_owner).any? { |k, v| k == "admin" && v.include?(record.organization) }
24
+ end
25
+
26
+ class Scope < ::Rhino::ViewerPolicy::Scope
27
+ def resolve
28
+ admin_organizations = ::Organization.joins(users_roles: [:role]).where(users_roles: { roles: { name: "admin" }, user: auth_owner })
29
+ scope.where(organization: admin_organizations).or(scope.where(user: auth_owner))
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ <p><%= t("devise.mailer.invitation_instructions.hello", email: @resource.email) %></p>
2
+
3
+ <p><%= t("devise.mailer.invitation_instructions.someone_invited_you", url: ENV['FRONT_END_URL']) %></p>
4
+
5
+ <p><%= link_to("Accept invitation", "#{ENV['FRONT_END_URL']}/auth/accept-invitation?invitation_token=#{@token}") %></p>
6
+
7
+ <% if @resource.invitation_due_at %>
8
+ <p><%= t("devise.mailer.invitation_instructions.accept_until", due_date: l(@resource.invitation_due_at, format: :'devise.mailer.invitation_instructions.accept_until_format')) %></p>
9
+ <% end %>
10
+
11
+ <p><%= t("devise.mailer.invitation_instructions.ignore") %></p>
@@ -0,0 +1,11 @@
1
+ <%= t("devise.mailer.invitation_instructions.hello", email: @resource.email) %>
2
+
3
+ <%= t("devise.mailer.invitation_instructions.someone_invited_you", url: ENV['FRONT_END_URL']) %>
4
+
5
+ <%= "#{ENV['FRONT_END_URL']}/auth/accept-invitation?invitation_token=#{@token}" %>
6
+
7
+ <% if @resource.invitation_due_at %>
8
+ <%= t("devise.mailer.invitation_instructions.accept_until", due_date: l(@resource.invitation_due_at, format: :'devise.mailer.invitation_instructions.accept_until_format')) %>
9
+ <% end %>
10
+
11
+ <%= t("devise.mailer.invitation_instructions.ignore") %>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,23 @@
1
+ class CreateOrganizations < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :organizations do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :roles do |t|
10
+ t.string :name, null: false
11
+
12
+ t.timestamps
13
+ end
14
+
15
+ create_table :users_roles do |t|
16
+ t.references :user, null: false, foreign_key: true
17
+ t.references :organization, null: false, foreign_key: true
18
+ t.references :role, null: false, foreign_key: true
19
+
20
+ t.timestamps
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ class AddDeviseInvitableToUsers < ActiveRecord::Migration[6.1]
2
+ def change
3
+ add_column :users, :invitation_token, :string
4
+ add_column :users, :invitation_created_at, :datetime
5
+ add_column :users, :invitation_sent_at, :datetime
6
+ add_column :users, :invitation_accepted_at, :datetime
7
+ add_column :users, :invitation_limit, :integer
8
+ add_column :users, :invited_by_id, :integer
9
+ add_column :users, :invited_by_type, :string
10
+ add_index :users, :invitation_token, unique: true
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsersRoleInvite < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :users_role_invites do |t|
4
+ t.string :email, null: false
5
+ t.references :organization, null: false, foreign_key: true
6
+ t.references :role, null: false, foreign_key: true
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RhinoOrganizations
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def install_models # rubocop:todo Metrics/MethodLength
9
+ say 'Copying rhino_organizations models to app/models'
10
+ copy_file "#{__dir__}/templates/models/organization.rb", 'app/models/organization.rb'
11
+ copy_file "#{__dir__}/templates/models/role.rb", 'app/models/role.rb'
12
+ copy_file "#{__dir__}/templates/models/users_role.rb", 'app/models/users_role.rb'
13
+ copy_file "#{__dir__}/templates/models/users_role_invite.rb", 'app/models/users_role_invite.rb'
14
+
15
+ data = <<-'RUBY'
16
+ has_many :users_roles, dependent: :destroy
17
+ has_many :organizations, through: :users_roles
18
+ has_many :roles, through: :users_roles
19
+ RUBY
20
+
21
+ inject_into_file 'app/models/user.rb', optimize_indentation(data, 2), after: "class User < Rhino::User\n"
22
+
23
+ data = <<-'RUBY'
24
+ has_many :users_roles, dependent: :destroy, foreign_key: :user_id, inverse_of: false
25
+ has_many :organizations, through: :users_roles
26
+ has_many :roles, through: :users_roles
27
+ RUBY
28
+ inject_into_file 'app/models/account.rb', optimize_indentation(data, 2), after: "class Account < Rhino::Account\n"
29
+ end
30
+
31
+ def install_devise_invitable
32
+ generate "devise_invitable:install"
33
+
34
+ # Set a sane default
35
+ gsub_file "config/initializers/devise.rb", /^ \# config.invitation_limit = 5/, " config.invitation_limit = 5"
36
+ end
37
+
38
+ def install_initializer
39
+ gsub_file 'config/initializers/rhino.rb', " # config.base_owner = 'Organization'", " config.base_owner = \"Organization\""
40
+
41
+ data = 'config.resources += ["Organization", "UsersRole", "Role", "UsersRoleInvite"]'
42
+ inject_into_file 'config/initializers/rhino.rb', optimize_indentation(data, 2), after: /^\s*config\.resources.+\n/
43
+ end
44
+
45
+ def install_active_admin # rubocop:disable Metrics/MethodLength
46
+ say 'Copying rhino_organization ActiveAdmin files and configurations'
47
+ copy_file "#{__dir__}/templates/admin/users_roles.rb", 'app/admin/users_roles.rb'
48
+ data = <<-'RUBY'
49
+ br
50
+ br
51
+ panel "User Roles" do
52
+ table_for user.users_roles do
53
+ column :organization
54
+ column :role
55
+ end
56
+ end
57
+ RUBY
58
+ inject_into_file 'app/admin/users.rb', optimize_indentation(data, 4), after: "default_main_content\n"
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ ActiveAdmin.register UsersRole do
4
+ permit_params :user_id, :organization_id, :role_id
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Organization < Rhino::Organization
4
+ has_many :users_roles, dependent: :destroy
5
+ has_many :users, through: :users_roles
6
+
7
+ rhino_references [{ users_roles: [:role] }]
8
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Role < Rhino::Role
4
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UsersRole < Rhino::UsersRole
4
+ belongs_to :user
5
+ belongs_to :organization
6
+ belongs_to :role
7
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UsersRoleInvite < Rhino::UsersRoleInvite
4
+ belongs_to :organization
5
+ belongs_to :role
6
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rhino/engine'
4
+
5
+ module RhinoOrganizations
6
+ class Engine < ::Rails::Engine
7
+ config.autoload_paths << File.expand_path('../../lib', __dir__)
8
+
9
+ initializer 'rhino_organizations.register_module' do
10
+ config.after_initialize do
11
+ if Rhino.resources.include?('Organization')
12
+ Rhino.registered_modules[:rhino_organizations] = {
13
+ version: RhinoOrganizations::VERSION
14
+ }
15
+ end
16
+ end
17
+ end
18
+
19
+ # https://guides.rubyonrails.org/engines.html#overriding-models-and-controllers
20
+ # Use root instead of Rails.root to scope for this engine
21
+ initializer 'rhino_organizations.overrides' do
22
+ overrides = "#{root}/app/overrides"
23
+ Rails.autoloaders.main.ignore(overrides)
24
+
25
+ config.to_prepare do
26
+ # FIXME: Only necessary because this module is in tree NUB-682
27
+ if Rhino.resources.include?('Organization')
28
+ Dir.glob("#{overrides}/**/*_override.rb").each do |override|
29
+ load override
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ module TestCase
5
+ class OrganizationControllerTest < ControllerTest
6
+ protected
7
+ def sign_in_with_organization(role = "admin")
8
+ @current_user = create :user
9
+ @current_organization = create :organization
10
+ @current_role = create :role, name: role
11
+ @current_user_role = create :users_role, role: @current_role, user: @current_user, organization: @current_organization
12
+ sign_in @current_user
13
+ end
14
+
15
+ def sign_in_with_organization_and_admin_user
16
+ sign_in_with_organization
17
+ end
18
+
19
+ def sign_in_with_organization_and_non_admin_user
20
+ sign_in_with_organization("regular")
21
+ end
22
+
23
+ def index(params: {}, headers: {})
24
+ get "/api/organizations", params:, headers:
25
+ end
26
+
27
+ def prepare
28
+ sign_in
29
+ index params: @params
30
+ end
31
+
32
+ def prepare_with_organization
33
+ sign_in_with_organization_and_admin_user
34
+ index params: @params
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ module TestCase
5
+ class OrganizationControllerPolicyTest < OrganizationControllerTest
6
+ protected
7
+ def sign_in_org_users_and_resource(role = "admin", resource = :blog)
8
+ sign_in_with_organization(role)
9
+
10
+ @resource_class = resource.to_s.classify.safe_constantize
11
+ @resource_collection_route = "#{@resource_class.model_name.route_key}_path"
12
+ @resource_singular_route = "#{@resource_class.model_name.singular_route_key}_path"
13
+ @resource = create resource, organization: @current_organization
14
+ @another_resource = create resource
15
+ end
16
+
17
+ def policy_index_success(expected_results = [@resource])
18
+ get_api send(@resource_collection_route)
19
+
20
+ assert_response_ok
21
+ assert_equal expected_results.count, parsed_response["total"]
22
+ expected_results.each_with_index { |result, idx| assert_equal result.title, parsed_response["results"][idx]["title"] }
23
+ end
24
+
25
+ def policy_show_success(resource = @resource)
26
+ get_api send(@resource_singular_route, resource)
27
+
28
+ assert_response_ok
29
+ assert_equal resource.id, parsed_response["id"]
30
+ assert_equal resource.title, parsed_response["title"]
31
+ end
32
+
33
+ def policy_show_fail(resource = @resource)
34
+ get_api send(@resource_singular_route, resource)
35
+
36
+ assert_response_not_found
37
+ end
38
+
39
+ def policy_create_success(create_attributes)
40
+ assert_difference "#{@resource_class}.count" do
41
+ post_api send(@resource_collection_route), params: create_attributes
42
+ end
43
+
44
+ assert_response_ok
45
+ end
46
+
47
+ def policy_create_fail(create_attributes)
48
+ assert_no_difference "#{@resource_class}.count" do
49
+ post_api send(@resource_collection_route), params: create_attributes
50
+ end
51
+
52
+ assert_response_forbidden
53
+ end
54
+
55
+ def policy_update_success(update_attributes, resource = @resource)
56
+ patch_api send(@resource_singular_route, resource), params: update_attributes
57
+
58
+ assert_response_ok
59
+ end
60
+
61
+ def policy_update_fail(update_attributes, resource = @resource)
62
+ patch_api send(@resource_singular_route, resource), params: update_attributes
63
+
64
+ assert_response_forbidden
65
+ end
66
+
67
+ def policy_destroy_success(resource = @resource)
68
+ assert_difference "#{@resource_class}.count", -1 do
69
+ delete_api send(@resource_singular_route, resource)
70
+ end
71
+
72
+ assert_response_ok
73
+ end
74
+
75
+ def policy_destroy_fail(resource = @resource)
76
+ assert_no_difference "Blog.count" do
77
+ delete_api send(@resource_singular_route, resource)
78
+ end
79
+
80
+ assert_response_forbidden
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rhino/test_case"
4
+ require_relative "test_case/organization_controller"
5
+ require_relative "test_case/organization_controller_policy"
6
+
7
+ module RhinoOrganizations
8
+ module TestCase
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RhinoOrganizations
4
+ # Returns the currently loaded version of Rhino core as a +Gem::Version+.
5
+ def self.gem_version
6
+ Gem::Version.new VERSION::STRING
7
+ end
8
+
9
+ module VERSION
10
+ MAJOR = 0
11
+ MINOR = 20
12
+ TINY = 0
13
+ PRE = "beta.22"
14
+
15
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rhino_organizations/engine"
4
+
5
+ module RhinoOrganizations
6
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :rhino_organizations do
4
+ # Prevent migration installation task from showing up twice.
5
+ if Rake::Task.task_defined?('rhino_organizations_engine:install:migrations')
6
+ Rake::Task['rhino_organizations_engine:install:migrations'].clear_comments
7
+ end
8
+
9
+ desc 'Install rhino_organizations'
10
+ task install: :environment do
11
+ Rake::Task['rhino_organizations_engine:install:migrations'].invoke if Rake::Task.task_defined?('rhino_organizations_engine:install:migrations')
12
+
13
+ Rails::Command.invoke :generate, ['rhino_organizations:install']
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rhino_project_organizations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.20.0.beta.22
5
+ platform: ruby
6
+ authors:
7
+ - JP Rosevear
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-27 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: 7.0.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 7.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 7.0.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 7.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rhino_project_core
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 0.20.0.beta.22
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 0.20.0.beta.22
47
+ description: ''
48
+ email:
49
+ - jprosevear@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - README.md
55
+ - Rakefile
56
+ - app/assets/config/rhino_organizations_manifest.js
57
+ - app/controllers/rhino/users_role_invite_controller.rb
58
+ - app/controllers/rhino_organizations/concerns/create_organization.rb
59
+ - app/models/rhino/organization.rb
60
+ - app/models/rhino/role.rb
61
+ - app/models/rhino/users_role.rb
62
+ - app/models/rhino/users_role_invite.rb
63
+ - app/overrides/devise/invitations_controller_override.rb
64
+ - app/overrides/models/rhino/account_override.rb
65
+ - app/overrides/models/rhino/users_role_override.rb
66
+ - app/policies/rhino/organization_policy.rb
67
+ - app/policies/rhino/users_role_policy.rb
68
+ - app/views/devise/mailer/invitation_instructions.html.erb
69
+ - app/views/devise/mailer/invitation_instructions.text.erb
70
+ - config/routes.rb
71
+ - db/migrate/20201129221747_create_organizations.rb
72
+ - db/migrate/20211202195016_add_devise_invitable_to_users.rb
73
+ - db/migrate/20211207212517_create_users_role_invite.rb
74
+ - lib/generators/rhino_organizations/install/install_generator.rb
75
+ - lib/generators/rhino_organizations/install/templates/admin/users_roles.rb
76
+ - lib/generators/rhino_organizations/install/templates/models/organization.rb
77
+ - lib/generators/rhino_organizations/install/templates/models/role.rb
78
+ - lib/generators/rhino_organizations/install/templates/models/users_role.rb
79
+ - lib/generators/rhino_organizations/install/templates/models/users_role_invite.rb
80
+ - lib/rhino_organizations/engine.rb
81
+ - lib/rhino_organizations/test_case.rb
82
+ - lib/rhino_organizations/test_case/organization_controller.rb
83
+ - lib/rhino_organizations/test_case/organization_controller_policy.rb
84
+ - lib/rhino_organizations/version.rb
85
+ - lib/rhino_project_organizations.rb
86
+ - lib/tasks/rhino_organizations.rake
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">"
103
+ - !ruby/object:Gem::Version
104
+ version: 1.3.1
105
+ requirements: []
106
+ rubygems_version: 3.3.26
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: ''
110
+ test_files: []