bpl-institution-management 0.0.2

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 (36) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +70 -0
  3. data/Gemfile +4 -0
  4. data/README.md +19 -0
  5. data/Rakefile +56 -0
  6. data/app/controllers/concerns/bpl/institution_management/institutions_behavior.rb +87 -0
  7. data/app/controllers/concerns/bpl/institution_management/user_institutions_behavior.rb +30 -0
  8. data/app/controllers/institutions_controller.rb +4 -0
  9. data/app/controllers/user_institutions_controller.rb +13 -0
  10. data/app/models/concerns/bpl/institution_management/user_institutions.rb +79 -0
  11. data/app/models/institution.rb +13 -0
  12. data/app/views/institutions/edit.html.erb +28 -0
  13. data/app/views/institutions/index.html.erb +11 -0
  14. data/app/views/institutions/new.html.erb +5 -0
  15. data/app/views/institutions/show.html.erb +19 -0
  16. data/bpl-institution-management.gemspec +24 -0
  17. data/config/routes.rb +7 -0
  18. data/lib/bpl-institution-management.rb +2 -0
  19. data/lib/bpl/institution_management.rb +11 -0
  20. data/lib/bpl/institution_management/version.rb +5 -0
  21. data/lib/generators/institutions/institutions_generator.rb +71 -0
  22. data/lib/generators/institutions/templates/migrations/user_institutions.rb +19 -0
  23. data/spec/.gitignore +1 -0
  24. data/spec/controllers/institutions_controller_spec.rb +117 -0
  25. data/spec/controllers/user_institutions_controller_spec.rb +61 -0
  26. data/spec/lib/user_institutions_spec.rb +26 -0
  27. data/spec/models/institution_spec.rb +35 -0
  28. data/spec/routing/institution_management_routes_spec.rb +25 -0
  29. data/spec/spec_helper.rb +14 -0
  30. data/spec/support/Gemfile +28 -0
  31. data/spec/support/app/models/sample.rb +37 -0
  32. data/spec/support/app/models/solr_document.rb +5 -0
  33. data/spec/support/config/initializers/hydra_config.rb +28 -0
  34. data/spec/support/lib/generators/test_app_generator.rb +47 -0
  35. data/spec/support/lib/tasks/rspec.rake +9 -0
  36. metadata +161 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b12ce3c38e5393d54480a92615e7b373b59c738
4
+ data.tar.gz: 40e1d905234af541aa6120a089b139dbcebc3128
5
+ SHA512:
6
+ metadata.gz: a4502986ef49b1f0d058d79615d33a969abc3a938aa4015a70c800f3292a8168ae4f05ed4d15cab6338b6c4a4b41a6a5a502472e743c9f4bb911b6ebb740324f
7
+ data.tar.gz: e056a3936d2f7732ed8e00c77372213f45af7ca5b815cf7a2fb94dc0a97578b4eae4e42b52bcc9cd3a2aa8b067acb8662cef4321eb41af0996b941d1c225f85a
@@ -0,0 +1,70 @@
1
+ # Generated by default by Github #
2
+ ##################################
3
+ *.rbc
4
+ *.sassc
5
+ .sass-cache
6
+ capybara-*.html
7
+ .rspec
8
+ /.bundle
9
+ /vendor/bundle
10
+ /log/*
11
+ /tmp/*
12
+ /db/*.sqlite3
13
+ /public/system/*
14
+ /coverage/
15
+ /spec/tmp/*
16
+ **.orig
17
+ rerun.txt
18
+ pickle-email-*.html
19
+
20
+ # Specific configuration files #
21
+ ################################
22
+ hydra-ldap.yml
23
+ database.yml
24
+ fedora.yml
25
+ solr.yml
26
+ role_map_cucumber.yml
27
+ role_map_development.yml
28
+ role_map_production.yml
29
+ role_map_test.yml
30
+ ark.yml
31
+ omniauth-polaris.yml
32
+ bpl_config.yml
33
+
34
+ # Files to ignore by default in Git #
35
+ #####################################
36
+ *.pdf
37
+ *.db
38
+ *.jar
39
+ *.gz
40
+ *.tar
41
+ *.zip
42
+ *.dll
43
+ *.sqlite
44
+ *.sql
45
+
46
+ # Specific Files to ignore #
47
+ ############################
48
+ Thumbs.db
49
+ Icon?
50
+ .DS_Store?
51
+ .DS_Store
52
+ ehthumbs.db
53
+ .idea # Rubymine config files
54
+ /.idea/*
55
+ Gemfile.lock
56
+
57
+ # Directories to ignore #
58
+ #########################
59
+ /public/data/*
60
+ /public/batch_uploads/*
61
+ /jetty/*
62
+ /solr_conf/*
63
+ /fedora_conf/*
64
+ /hydra-jetty/*
65
+ /*jetty*
66
+
67
+ # Ignore edited Linux files #
68
+ #############################
69
+
70
+ *~
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hydra-batch-edit.gemspec
4
+ gemspec
@@ -0,0 +1,19 @@
1
+ An engine gem to provide a RDBMS backed list of roles and their associated user. This replaces the hydra default role mapper.
2
+
3
+
4
+ ##Installing:
5
+
6
+ * Add: ```gem 'bpl-institution-management'``` to your Gemfile and then ```bundle install```
7
+ * ```rails generate roles```
8
+ * ```rake db:migrate```
9
+ * Add the following [cancan](https://github.com/ryanb/cancan) abilities:
10
+
11
+ ```ruby
12
+ # app/models/ability.rb
13
+ TBA
14
+ ```
15
+
16
+ ##Testing:
17
+
18
+ * Install a system javascript runtime or uncomment therubyracer in spec/support/Gemfile
19
+ * Run ```rake generate spec``` to generate a test rails app at spec/internal and test it
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ ENV["RAILS_ROOT"] ||= 'spec/internal'
6
+
7
+ desc 'Default: run specs.'
8
+ task :default => :spec
9
+
10
+
11
+ task :spec => [:generate] do |t|
12
+ focused_spec = ENV['SPEC'] ? " SPEC=#{File.join(GEM_ROOT, ENV['SPEC'])}" : ''
13
+ within_test_app do
14
+ system "rake myspec#{focused_spec}"
15
+ abort "Error running bpl-institution-management" unless $?.success?
16
+ end
17
+ end
18
+
19
+
20
+
21
+ desc "Create the test rails app"
22
+ task :generate do
23
+ unless File.exists?('spec/internal/Rakefile')
24
+ puts "Generating rails app"
25
+ `rails new spec/internal`
26
+ puts "Copying gemfile"
27
+ `cp spec/support/Gemfile spec/internal`
28
+ puts "Copying generator"
29
+ `cp -r spec/support/lib/generators spec/internal/lib`
30
+
31
+ within_test_app do
32
+ puts "Bundle install"
33
+ puts `bundle install`
34
+ puts "running generator"
35
+ puts `rails generate test_app`
36
+
37
+ puts "running migrations"
38
+ puts `rake db:migrate db:test:prepare`
39
+ end
40
+ end
41
+ puts "Running specs"
42
+ end
43
+
44
+ desc "Clean out the test rails app"
45
+ task :clean do
46
+ puts "Removing sample rails app"
47
+ `rm -rf spec/internal`
48
+ end
49
+
50
+ def within_test_app
51
+ FileUtils.cd('spec/internal')
52
+ Bundler.with_clean_env do
53
+ yield
54
+ end
55
+ FileUtils.cd('../..')
56
+ end
@@ -0,0 +1,87 @@
1
+ module Bpl
2
+ module InstitutionManagement
3
+ module InstitutionsBehavior
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ load_and_authorize_resource
8
+ end
9
+
10
+ def index
11
+ end
12
+
13
+ def show
14
+ redirect_to institution_management.edit_institution_path(@institution) if can? :edit, @institution
15
+ end
16
+
17
+ def new
18
+ end
19
+
20
+ def edit
21
+ end
22
+
23
+ def create
24
+
25
+ @institution = Institution.new(institution_params)
26
+ #@institution.name = params[:institution][:name]
27
+
28
+ institution = Bplmodels::Institution.find(:label_ssim=>@institution.name).first
29
+ if institution == nil
30
+ @localid = @institution.name
31
+ @localid_type = "Physical Location From UI"
32
+
33
+ response = Typhoeus::Request.post(ARK_CONFIG_GLOBAL['url'] + "/arks.json", :params => {:ark=>{:namespace_ark => ARK_CONFIG_GLOBAL['namespace_commonwealth_ark'], :namespace_id=>ARK_CONFIG_GLOBAL['namespace_commonwealth_pid'], :url_base => ARK_CONFIG_GLOBAL['ark_commonwealth_base'], :model_type => Bplmodels::Institution.name, :local_original_identifier=>@localid, :local_original_identifier_type=>@localid_type}})
34
+
35
+ as_json = JSON.parse(response.body)
36
+
37
+ institution = Bplmodels::Institution.new(:pid=>as_json["pid"])
38
+ institution.label = @institution.name
39
+ institution.descMetadata.insert_title(@institution.name, nil)
40
+
41
+ institution.save!
42
+ end
43
+
44
+ @institution.pid = institution.pid
45
+
46
+ if (@institution.save)
47
+ redirect_to institution_management.edit_institution_path(@institution), notice: 'Institution was successfully created.'
48
+ else
49
+ render action: "new"
50
+ end
51
+ end
52
+
53
+ def update
54
+ @institution = Institution.find(params[:id])
55
+
56
+ institution = Bplmodels::Institution.find(:label_ssim=>@institution.name).first
57
+ institution.label = @institution.name
58
+ institution.save!
59
+
60
+ if (@institution.update_attributes(institution_params))
61
+ redirect_to institution_management.edit_institution_path(@institution), notice: 'Institution was successfully updated.'
62
+ else
63
+ render action: "edit"
64
+ end
65
+ end
66
+
67
+ def destroy
68
+ institution = Bplmodels::Institution.find(:label_ssim=>@institution.name).first
69
+ institution.label = @institution.name
70
+ institution.delete
71
+
72
+ if (@institution.destroy)
73
+ redirect_to institution_management.institutions_path, notice: 'Institution was successfully deleted.'
74
+ else
75
+ redirect_to institution_management.institutions_path
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def institution_params
82
+ params.require(:institution).permit(:name, :pid)
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,30 @@
1
+ module Bpl
2
+ module InstitutionManagement
3
+ module UserInstitutionsBehavior
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ load_and_authorize_resource :institution
8
+ end
9
+
10
+ def create
11
+ authorize! :add_user, @institution
12
+ u = ::User.find_by_user_key(params[:user_key])
13
+ if u
14
+ u.institutions << @institution
15
+ u.save!
16
+ redirect_to institution_management.institution_path(@institution)
17
+ else
18
+ redirect_to institution_management.institution_path(@institution), :flash=> {:error=>"Unable to find the user #{params[:user_key]}"}
19
+ end
20
+ end
21
+
22
+ def destroy
23
+ authorize! :remove_user, @institution
24
+ @institution.users.delete(::User.find(params[:id]))
25
+ redirect_to institution_management.institution_path(@institution)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,4 @@
1
+ class InstitutionsController < ApplicationController
2
+ include Bpl::InstitutionManagement::InstitutionsBehavior
3
+ end
4
+
@@ -0,0 +1,13 @@
1
+ class UserInstitutionsController < ApplicationController
2
+
3
+ #FIXME: Done to satisfy CanCan. See: http://stackoverflow.com/questions/12756469/cancan-load-and-authorize-resource-triggers-forbidden-attributes or http://stackoverflow.com/questions/20150322/rails-4-strong-params-forbiddenattributeserror
4
+ before_filter :fixCanCan, only: [:create, :edit]
5
+
6
+ def fixCanCan
7
+ params[:institution] = institution_params
8
+ end
9
+
10
+ include Bpl::InstitutionManagement::UserInstitutionsBehavior
11
+ end
12
+
13
+
@@ -0,0 +1,79 @@
1
+ module Bpl
2
+ module InstitutionManagement
3
+ module UserInstitutions
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ has_and_belongs_to_many :institutions
7
+ end
8
+
9
+
10
+ def institution_objects
11
+ Bplmodels::Institution.find(self.list_institution_pids)
12
+ end
13
+
14
+ def list_institution_pids
15
+ if superuser?
16
+ #return all pids if superuser
17
+ Institution.pluck(:pid)
18
+ else
19
+ #return only associated pids if normal user
20
+ institutions.pluck(:pid)
21
+ end
22
+
23
+ end
24
+
25
+ def list_institution_names
26
+ if superuser?
27
+ #return all pids if superuser
28
+ Institution.pluck(:name)
29
+ else
30
+ #return only associated pids if normal user
31
+ institutions.pluck(:name)
32
+ end
33
+
34
+ end
35
+
36
+ def list_collection_pids
37
+ collection_list = []
38
+ if superuser?
39
+ #return all pids if superuser
40
+ Bplmodels::Collection.find_in_batches("has_model_ssim"=>"info:fedora/afmodel:Bplmodels_Collection") do |collection_group|
41
+ collection_group.each { |solr_objects|
42
+ pid = solr_objects['id']
43
+ collection_list << pid
44
+ }
45
+ end
46
+
47
+ else
48
+ #return only associated pids if normal user
49
+ self.institution_objects.each do |institution|
50
+ institution.collections.each do |collection|
51
+ collection_list << collection.pid
52
+ end
53
+ end
54
+ end
55
+ collection_list
56
+ end
57
+
58
+ def list_collection_names
59
+ collection_list = []
60
+ if superuser?
61
+ #return all pids if superuser
62
+
63
+ Bplmodels::Collection.all.each do |collection|
64
+ collection_list << collection.label
65
+ end
66
+
67
+ else
68
+ #return only associated pids if normal user
69
+ self.institution_objects.collections.each do |collection|
70
+ collection_list << collection.label
71
+ end
72
+
73
+ end
74
+ collection_list
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,13 @@
1
+ class Institution < ActiveRecord::Base
2
+ has_and_belongs_to_many :users
3
+
4
+ #attr_accessible :pid, :name
5
+
6
+ validates :name,
7
+ uniqueness: true,
8
+ format: { with: /\A[a-zA-Z0-9._ -]+\z/,
9
+ :message => "Only letters, numbers, hyphens, underscores, spaces and periods are allowed"}
10
+
11
+ end
12
+
13
+
@@ -0,0 +1,28 @@
1
+ <h2>Institution:</h2>
2
+ <%= bootstrap_form_for @institution, :url=>institution_management.institution_path(@institution) do |f| %>
3
+ <%= f.text_field :name, :label=> 'Institution name' %>
4
+ <%= f.actions do %>
5
+ <%= f.submit "Update" %>
6
+ <% end %>
7
+ <% end %>
8
+ <% if can? :destroy, Institution %>
9
+ <%= button_to "Delete", institution_management.institution_path(@institution), :method=>:delete, :class=>'btn btn-danger' %>
10
+ <% end %>
11
+ <h3>Accounts:</h3>
12
+ <ul>
13
+ <% @institution.users.each do |user| %>
14
+ <li><%= user.user_key %>
15
+ <% if can? :remove_user, Institution %>
16
+ <%= button_to "Remove User", institution_management.institution_user_path(@institution, user), :method=>:delete, :class=>'btn btn-danger' %>
17
+ <% end %>
18
+ </li>
19
+ <% end %>
20
+ </ul>
21
+ <h3>Add a new account:</h3>
22
+ <%= bootstrap_form_tag institution_management.institution_users_path(@institution) do %>
23
+ <%= bootstrap_text_field_tag 'user_key', '', :label=>'User' %>
24
+ <%= bootstrap_actions do %>
25
+ <%= bootstrap_submit_tag "Add" %>
26
+ <%= bootstrap_cancel_tag %>
27
+ <% end %>
28
+ <% end %>
@@ -0,0 +1,11 @@
1
+ <h2>Institution</h2>
2
+ <ul>
3
+ <% @institutions.each do |institution| %>
4
+ <li><%=link_to institution.name, institution_management.institution_path(institution) %></li>
5
+ <% end %>
6
+ </ul>
7
+
8
+ <% if can? :create, Institution %>
9
+ <%= button_to "Create a new institution", institution_management.new_institution_path, method: :get, class: 'btn btn-primary' %>
10
+ <% end %>
11
+
@@ -0,0 +1,5 @@
1
+ <%= bootstrap_form_for @institution, :url=>institution_management.institutions_path do |f| %>
2
+ <%= f.text_field :name, :label=> 'Institution name' %>
3
+ <%= f.actions %>
4
+ <% end %>
5
+
@@ -0,0 +1,19 @@
1
+ <h2>Institution: <%= @institution.name %></h2>
2
+ <h3>Accounts:</h3>
3
+ <ul>
4
+ <% @institution.users.each do |user| %>
5
+ <li><%= user.user_key %>
6
+ <% if can? :remove_user, Institution %>
7
+ <%= button_to "Remove User", institution_management.institution_user_path(@institution, user), :method=>:delete, :class=>'btn btn-danger' %>
8
+ <% end %>
9
+ </li>
10
+ <% end %>
11
+ </ul>
12
+ <h3>Add a new account:</h3>
13
+ <%= bootstrap_form_tag institution_management.institution_users_path(@institution) do %>
14
+ <%= bootstrap_text_field_tag 'user_key', '', :label=>'User' %>
15
+ <%= bootstrap_actions do %>
16
+ <%= bootstrap_submit_tag "Add" %>
17
+ <%= bootstrap_cancel_tag %>
18
+ <% end %>
19
+ <% end %>
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bpl/institution_management/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Steven Anderson"]
6
+ gem.email = ["sanderson@bpl.org"]
7
+ gem.description = %q{Rails engine to do user institutions in an RDBMS for hydra-head}
8
+ gem.summary = %q{Rails engine to do user institutions in an RDBMS for hydra-head}
9
+ gem.homepage = "https://github.com/boston-library/bpl-institution-management"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "bpl-institution-management"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Bpl::InstitutionManagement::VERSION
17
+
18
+ gem.add_dependency 'cancan'
19
+ gem.add_dependency 'bootstrap_forms'
20
+
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'rails'
23
+ gem.add_development_dependency 'rspec-rails'
24
+ end
@@ -0,0 +1,7 @@
1
+ Bpl::InstitutionManagement::Engine.routes.draw do
2
+ # Generic file routes
3
+ resources :institutions do
4
+ resources :users, :only=>[:create, :destroy], :controller => "user_institutions"
5
+ end
6
+ end
7
+
@@ -0,0 +1,2 @@
1
+ require "bpl/institution_management"
2
+ require 'bootstrap_forms'
@@ -0,0 +1,11 @@
1
+ module Bpl
2
+ module InstitutionManagement
3
+ class Engine < ::Rails::Engine
4
+ engine_name 'institution_management'
5
+
6
+ # Rails 4 should do this automatically:
7
+ config.paths.add "app/controllers/concerns", eager_load: true
8
+ config.paths.add "app/models/concerns", eager_load: true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Bpl
2
+ module InstitutionManagement
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,71 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'rails/generators'
3
+ require 'rails/generators/migration'
4
+
5
+ class InstitutionsGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ argument :model_name, :type => :string , :default => "user"
11
+ desc """
12
+ This generator makes the following changes to your application:
13
+ 1. Creates several database migrations if they do not exist in /db/migrate
14
+ 2. Adds user behavior to the user model
15
+ 2. Adds routes
16
+ """
17
+
18
+ # Implement the required interface for Rails::Generators::Migration.
19
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
20
+ def self.next_migration_number(path)
21
+ unless @prev_migration_nr
22
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
23
+ else
24
+ @prev_migration_nr += 1
25
+ end
26
+ @prev_migration_nr.to_s
27
+ end
28
+
29
+ # Setup the database migrations
30
+ def copy_migrations
31
+ # Can't get this any more DRY, because we need this order.
32
+ %w{user_institutions.rb}.each do |f|
33
+ better_migration_template f
34
+ end
35
+ end
36
+
37
+ # Add behaviors to the user model
38
+ def inject_user_roles_behavior
39
+ file_path = "app/models/#{model_name.underscore}.rb"
40
+ if File.exists?(file_path)
41
+ code = "\n # Connects this user object to Institution-management behaviors. " +
42
+ "\n include Bpl::InstitutionManagement::UserInstitutions\n"
43
+ inject_into_file file_path, code, { :after => /include Hydra::User/ }
44
+ else
45
+ puts " \e[31mFailure\e[0m bpl-institution-management requires a user object. This generators assumes that the model is defined in the file #{file_path}, which does not exist. If you used a different name, please re-run the generator and provide that name as an argument. Such as \b rails -g roles client"
46
+ end
47
+ end
48
+
49
+
50
+ # The engine routes have to come after the devise routes so that /users/sign_in will work
51
+ def inject_routes
52
+ routing_code = "mount Bpl::InstitutionManagement::Engine => '/'"
53
+ sentinel = /devise_for :users/
54
+ inject_into_file 'config/routes.rb', "\n #{routing_code}\n", { :after => sentinel, :verbose => false }
55
+
56
+ end
57
+
58
+ private
59
+
60
+ def better_migration_template (file)
61
+ begin
62
+ migration_template "migrations/#{file}", "db/migrate/#{file}"
63
+ sleep 1 # ensure scripts have different time stamps
64
+ rescue
65
+ puts " \e[1m\e[34mMigrations\e[0m " + $!.message
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+
@@ -0,0 +1,19 @@
1
+ class UserInstitutions < ActiveRecord::Migration
2
+ def up
3
+ create_table :institutions do |t|
4
+ t.string :name
5
+ t.string :pid
6
+ end
7
+ create_table :institutions_users, :id => false do |t|
8
+ t.references :institution
9
+ t.references :user
10
+ end
11
+ add_index :institutions_users, [:institution_id, :user_id]
12
+ add_index :institutions_users, [:user_id, :institution_id]
13
+ end
14
+
15
+ def down
16
+ drop_table :institutions_users
17
+ drop_table :institutions
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ /internal
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe InstitutionController do
4
+ let(:ability) do
5
+ ability = Object.new
6
+ ability.extend(CanCan::Ability)
7
+ controller.stub(:current_ability).and_return(ability)
8
+ ability
9
+ end
10
+
11
+ let(:institution) do
12
+ Institution.create(name: 'foo')
13
+ end
14
+
15
+
16
+ describe "with a user who cannot edit institutions" do
17
+ it "should not be able to view institution index" do
18
+ lambda { get :index }.should raise_error CanCan::AccessDenied
19
+ end
20
+ it "should not be able to view institution" do
21
+ lambda { get :show, id: institution }.should raise_error CanCan::AccessDenied
22
+ end
23
+ it "should not be able to view new institution form" do
24
+ lambda { get :new }.should raise_error CanCan::AccessDenied
25
+ end
26
+ it "should not be able to create a institution" do
27
+ lambda { post :create, :institution=>{name: 'my_institution'}}.should raise_error CanCan::AccessDenied
28
+ end
29
+ it "should not be able to update a institution" do
30
+ lambda { put :update, id: institution}.should raise_error CanCan::AccessDenied
31
+ end
32
+ it "should not be able to remove a institution" do
33
+ lambda { delete :destroy, id: institution}.should raise_error CanCan::AccessDenied
34
+ end
35
+ end
36
+
37
+ describe "with a user who can read institutions" do
38
+ before do
39
+ ability.can :read, Institution
40
+ end
41
+ it "should be able to see the list of institutions" do
42
+ get :index
43
+ response.should be_successful
44
+ assigns[:institutions].should == [institution]
45
+ end
46
+
47
+ it "should be able to see a single institution" do
48
+ get :show, id: institution
49
+ response.should be_successful
50
+ assigns[:institution].should == institution
51
+ end
52
+ end
53
+
54
+ describe "with a user who can only update institution 'foo'" do
55
+ it "should be redirected to edit" do
56
+ ability.can :read, Institution
57
+ ability.can :update, Institution, id: institution.id
58
+ get :show, id: institution
59
+ response.should redirect_to @routes.url_helpers.edit_institution_path(assigns[:institution])
60
+ end
61
+ end
62
+
63
+ describe "with a user who can create institutions" do
64
+ before do
65
+ ability.can :create, Institution
66
+ end
67
+ it "should be able to make a new institution" do
68
+ get :new
69
+ response.should be_successful
70
+ assigns[:institution].should be_kind_of Institution
71
+ end
72
+
73
+ it "should be able to create a new institution" do
74
+ post :create, :institution=>{name: 'my_institution'}
75
+ response.should redirect_to @routes.url_helpers.edit_institution_path(assigns[:institution])
76
+ assigns[:institution].should_not be_new_record
77
+ assigns[:institution].name.should == 'my_institution'
78
+ end
79
+ it "should not create institution with an error" do
80
+ post :create, :institution=>{name: 'my institution'}
81
+ assigns[:institution].name.should == 'my institution'
82
+ assigns[:institution].errors[:name].should == ['Only letters, numbers, hyphens, underscores and periods are allowed']
83
+ response.should be_successful
84
+ end
85
+ end
86
+
87
+ describe "with a user who can update institutions" do
88
+ before do
89
+ ability.can :update, Institution
90
+ end
91
+
92
+ it "should be able to update a institution" do
93
+ put :update, id: institution, :institution=>{name: 'my_institution'}
94
+ response.should redirect_to @routes.url_helpers.edit_institution_path(assigns[:institution])
95
+ assigns[:institution].should_not be_new_record
96
+ assigns[:institution].name.should == 'my_institution'
97
+ end
98
+ it "should not update institution with an error" do
99
+ put :update, id: institution, :institution=>{name: 'my institution'}
100
+ assigns[:institution].name.should == 'my institution'
101
+ assigns[:institution].errors[:name].should == ['Only letters, numbers, hyphens, underscores and periods are allowed']
102
+ response.should be_successful
103
+ end
104
+ end
105
+
106
+ describe "with a user who can remove institutions" do
107
+ before do
108
+ ability.can :destroy, Institution
109
+ end
110
+
111
+ it "should be able to destroy a institution" do
112
+ delete :destroy, id: institution
113
+ response.should redirect_to @routes.url_helpers.institutions_path
114
+ end
115
+ end
116
+
117
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe UserInstitutionsController do
4
+ let(:ability) do
5
+ ability = Object.new
6
+ ability.extend(CanCan::Ability)
7
+ controller.stub(:current_ability).and_return(ability)
8
+ ability
9
+ end
10
+
11
+ let(:institution) do
12
+ Institution.create(name: 'foo')
13
+ end
14
+
15
+ describe "with a user who cannot edit users" do
16
+ it "should not be able to add a user" do
17
+ lambda { post :create, institution_id: institution, user_key: 'foo@example.com'}.should raise_error CanCan::AccessDenied
18
+ end
19
+ it "should not be able to remove a user" do
20
+ lambda { delete :destroy, institution_id: institution, id: 7}.should raise_error CanCan::AccessDenied
21
+ end
22
+ end
23
+
24
+ describe "with a user who can edit users" do
25
+ before do
26
+ ability.can :read, Institution
27
+ end
28
+ describe "adding users" do
29
+ before do
30
+ ability.can :add_user, Institution
31
+ end
32
+ it "should not be able to add a user that doesn't exist" do
33
+ User.should_receive(:find_by_user_key).with('foo@example.com').and_return(nil)
34
+ post :create, institution_id: institution, user_key: 'foo@example.com'
35
+ flash[:error].should == "Unable to find the user foo@example.com"
36
+ end
37
+ it "should be able to add a user" do
38
+ u = User.create!(email: 'foo@example.com', password: 'password', password_confirmation: 'password')
39
+ post :create, institution_id: institution, user_key: 'foo@example.com'
40
+ institution.reload.users.should == [u]
41
+ end
42
+ end
43
+ describe "removing users" do
44
+ before do
45
+ ability.can :remove_user, Institution
46
+ end
47
+ let (:user) do
48
+ u = User.new(email: 'foo@example.com', password: 'password', password_confirmation: 'password')
49
+ u.institutions = [institution]
50
+ u.save!
51
+ u
52
+ end
53
+ it "should be able to remove a user" do
54
+ user.institutions.should == [institution]
55
+ delete :destroy, institution_id: institution, id: user.id
56
+ institution.reload.users.should == []
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bpl::InstitutionManagement::UserInstitutions do
4
+ subject do
5
+ User.create!(email: 'fred@example.com', password: 'password')
6
+ end
7
+
8
+ it "should have admin?" do
9
+ subject.should_not be_admin
10
+ end
11
+
12
+ it "should have institutions" do
13
+ subject.institutions.should == []
14
+ subject.institutions << Institution.create!(name: 'librarian')
15
+ subject.institutions.first.name.should == 'librarian'
16
+
17
+ end
18
+
19
+ it "should have groups" do
20
+ subject.institutions.should == []
21
+ subject.institutions << Institution.create!(name: 'librarian')
22
+ subject.save!
23
+ subject.groups.should include('registered', 'librarian')
24
+ end
25
+
26
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Institution do
4
+ it "should require a name" do
5
+ subject.should_not be_valid
6
+ subject.name = 'foo'
7
+ subject.should be_valid
8
+ end
9
+
10
+ it "should not allow space in the name" do
11
+ subject.name = 'foo bar'
12
+ subject.should_not be_valid
13
+ end
14
+
15
+ it "should not allow comma in the name" do
16
+ subject.name = 'foo,bar'
17
+ subject.should_not be_valid
18
+ end
19
+
20
+ it "should not allow ampersand in the name" do
21
+ subject.name = 'foo&bar'
22
+ subject.should_not be_valid
23
+ end
24
+
25
+ it "should not allow less-than in the name" do
26
+ subject.name = 'foo<bar'
27
+ subject.should_not be_valid
28
+ end
29
+
30
+ it "should validate uniqueness" do
31
+ subject.name ='foo'
32
+ subject.save!
33
+ Institution.new(name: 'foo').should_not be_valid
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Routes for institution_management" do
4
+ before(:each) do
5
+ @routes = Bpl::InstitutionManagement::Engine.routes
6
+ # so we have to do this instead:
7
+ # engine routes broke in rspec rails 2.12.1, so we had to add this:
8
+ assertion_instance.instance_variable_set(:@routes, @routes)
9
+ end
10
+ it "should route index" do
11
+ { :get => '/institutions' }.should route_to( :controller => "institutions", :action => "index")
12
+ end
13
+ it "should create institutions" do
14
+ { :post => '/institutions' }.should route_to( :controller => "institutions", :action => "create")
15
+ end
16
+ it "should show institutions" do
17
+ { :get => '/institutions/7' }.should route_to( :controller => "institutions", :action => "show", :id => '7')
18
+ end
19
+ it "should add users" do
20
+ { :post => '/institutions/7/users' }.should route_to( :controller => "user_institutions", :institution_id=>'7', :action => "create")
21
+ end
22
+ it "should remove users" do
23
+ { :delete => '/institutions/7/users/5' }.should route_to( :controller => "user_institutions", :institution_id=>'7', :id=>'5', :action => "destroy")
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ ENV["RAILS_ENV"] ||= 'test'
4
+
5
+ require File.expand_path("config/environment", ENV['RAILS_ROOT'] || File.expand_path("../internal", __FILE__))
6
+ require 'rspec/rails'
7
+
8
+
9
+ RSpec.configure do |config|
10
+ config.use_transactional_fixtures = true
11
+ config.before(:each, :type=>"controller") { @routes = Bpl::InstitutionManagement::Engine.routes }
12
+ config.include Devise::TestHelpers, :type => :controller
13
+
14
+ end
@@ -0,0 +1,28 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails'
4
+
5
+ # Bundle edge Rails instead:
6
+ # gem 'rails', :git => 'git://github.com/rails/rails.git'
7
+
8
+ gem 'sqlite3'
9
+ gem 'blacklight'
10
+ gem 'hydra-head'
11
+
12
+
13
+ # Gems used only for assets and not required
14
+ # in production environments by default.
15
+ group :assets do
16
+ gem 'sass-rails', '~> 3.2.3'
17
+ gem 'coffee-rails', '~> 3.2.1'
18
+
19
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
20
+ #gem 'therubyracer', :platforms => :ruby
21
+
22
+ gem 'uglifier', '>= 1.0.3'
23
+ end
24
+
25
+ gem 'jquery-rails'
26
+
27
+ gem 'hydra-role-management', :path=>'../../'
28
+ gem 'rspec-rails'
@@ -0,0 +1,37 @@
1
+ class Sample
2
+ # This is a stub model for testing.
3
+
4
+ cattr_accessor :objects
5
+ self.objects = {}
6
+
7
+ def self.create(params={})
8
+ obj = Sample.new
9
+ obj.save
10
+ obj
11
+ end
12
+
13
+ def save()
14
+ @pid ||= "sample:#{(rand * 1000).to_i}"
15
+ self.class.objects[@pid] = self
16
+ end
17
+
18
+ def update_attributes(attributes)
19
+ attributes.each do |k, v|
20
+ instance_variable_set "@#{k.to_s}".to_sym, v
21
+
22
+ self.class.send :attr_accessor, k
23
+ end
24
+ end
25
+
26
+ def self.find(pid)
27
+ objects[pid]
28
+ end
29
+
30
+ def pid
31
+ @pid
32
+ end
33
+
34
+ def destroy
35
+ self.class.objects.delete(@pid)
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class SolrDocument
3
+
4
+ include Blacklight::Solr::Document
5
+ end
@@ -0,0 +1,28 @@
1
+ # The following lines determine which user attributes your hydrangea app will use
2
+ # This configuration allows you to use the out of the box ActiveRecord associations between users and user_attributes
3
+ # It also allows you to specify your own user attributes
4
+ # The easiest way to override these methods would be to create your own module to include in User
5
+ # For example you could create a module for your local LDAP instance called MyLocalLDAPUserAttributes:
6
+ # User.send(:include, MyLocalLDAPAttributes)
7
+ # As long as your module includes methods for full_name, affiliation, and photo the personalization_helper should function correctly
8
+ #
9
+
10
+ # windows doesn't properly require hydra-head (from the gemfile), so we need to require it explicitly here:
11
+ require 'hydra/head' unless defined? Hydra
12
+
13
+ if Hydra.respond_to?(:configure)
14
+ Hydra.configure(:shared) do |config|
15
+ # This specifies the solr field names of permissions-related fields.
16
+ # You only need to change these values if you've indexed permissions by some means other than the Hydra's built-in tooling.
17
+ # If you change these, you must also update the permissions request handler in your solrconfig.xml to return those values
18
+ indexer = Solrizer::Descriptor.new(:string, :stored, :indexed, :multivalued)
19
+ config[:permissions] = {
20
+ :discover => {:group =>ActiveFedora::SolrService.solr_name("discover_access_group", indexer), :individual=>ActiveFedora::SolrService.solr_name("discover_access_person", indexer)},
21
+ :read => {:group =>ActiveFedora::SolrService.solr_name("read_access_group", indexer), :individual=>ActiveFedora::SolrService.solr_name("read_access_person", indexer)},
22
+ :edit => {:group =>ActiveFedora::SolrService.solr_name("edit_access_group", indexer), :individual=>ActiveFedora::SolrService.solr_name("edit_access_person", indexer)},
23
+ :owner => ActiveFedora::SolrService.solr_name("depositor", indexer),
24
+ :embargo_release_date => ActiveFedora::SolrService.solr_name("embargo_release_date", Solrizer::Descriptor.new(:date, :stored, :indexed))
25
+ }
26
+
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails/generators'
2
+
3
+ class TestAppGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../../../support", __FILE__)
5
+
6
+ # # Inject call to Hydra::BatchEdit.add_routes in config/routes.rb
7
+ # def inject_routes
8
+ # insert_into_file "config/routes.rb", :after => '.draw do' do
9
+ # "\n # Add BatchEdit routes."
10
+ # "\n Hydra::BatchEdit.add_routes(self)"
11
+ # end
12
+ # end
13
+
14
+
15
+ def run_blacklight_generator
16
+ say_status("warning", "GENERATING BL", :yellow)
17
+
18
+ generate 'blacklight', '--devise'
19
+ end
20
+
21
+ def run_hydra_head_generator
22
+ say_status("warning", "GENERATING HH", :yellow)
23
+
24
+ generate 'hydra:head', '-f'
25
+ end
26
+
27
+ def run_roles_generator
28
+ say_status("warning", "GENERATING ROLES", :yellow)
29
+
30
+ generate 'roles', '-f'
31
+
32
+ end
33
+
34
+ # def copy_test_models
35
+ # copy_file "app/models/sample.rb"
36
+ # copy_file "app/models/solr_document.rb"
37
+ # copy_file "db/migrate/20111101221803_create_searches.rb"
38
+ # end
39
+
40
+ def copy_rspec_rake_task
41
+ copy_file "lib/tasks/rspec.rake"
42
+ end
43
+
44
+ def copy_hydra_config
45
+ copy_file "config/initializers/hydra_config.rb"
46
+ end
47
+ end
@@ -0,0 +1,9 @@
1
+ require 'rspec/core/rake_task'
2
+ desc "run the hydra-batch-edit gem spec"
3
+ gem_home = File.expand_path('../../../../..', __FILE__)
4
+ RSpec::Core::RakeTask.new(:myspec) do |t|
5
+ t.pattern = gem_home + '/spec/**/*_spec.rb'
6
+ t.rspec_opts = "--colour"
7
+ t.ruby_opts = "-I#{gem_home}/spec"
8
+ end
9
+
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bpl-institution-management
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Steven Anderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cancan
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: bootstrap_forms
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Rails engine to do user institutions in an RDBMS for hydra-head
84
+ email:
85
+ - sanderson@bpl.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - app/controllers/concerns/bpl/institution_management/institutions_behavior.rb
95
+ - app/controllers/concerns/bpl/institution_management/user_institutions_behavior.rb
96
+ - app/controllers/institutions_controller.rb
97
+ - app/controllers/user_institutions_controller.rb
98
+ - app/models/concerns/bpl/institution_management/user_institutions.rb
99
+ - app/models/institution.rb
100
+ - app/views/institutions/edit.html.erb
101
+ - app/views/institutions/index.html.erb
102
+ - app/views/institutions/new.html.erb
103
+ - app/views/institutions/show.html.erb
104
+ - bpl-institution-management.gemspec
105
+ - config/routes.rb
106
+ - lib/bpl-institution-management.rb
107
+ - lib/bpl/institution_management.rb
108
+ - lib/bpl/institution_management/version.rb
109
+ - lib/generators/institutions/institutions_generator.rb
110
+ - lib/generators/institutions/templates/migrations/user_institutions.rb
111
+ - spec/.gitignore
112
+ - spec/controllers/institutions_controller_spec.rb
113
+ - spec/controllers/user_institutions_controller_spec.rb
114
+ - spec/lib/user_institutions_spec.rb
115
+ - spec/models/institution_spec.rb
116
+ - spec/routing/institution_management_routes_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/support/Gemfile
119
+ - spec/support/app/models/sample.rb
120
+ - spec/support/app/models/solr_document.rb
121
+ - spec/support/config/initializers/hydra_config.rb
122
+ - spec/support/lib/generators/test_app_generator.rb
123
+ - spec/support/lib/tasks/rspec.rake
124
+ homepage: https://github.com/boston-library/bpl-institution-management
125
+ licenses: []
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.0.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Rails engine to do user institutions in an RDBMS for hydra-head
147
+ test_files:
148
+ - spec/.gitignore
149
+ - spec/controllers/institutions_controller_spec.rb
150
+ - spec/controllers/user_institutions_controller_spec.rb
151
+ - spec/lib/user_institutions_spec.rb
152
+ - spec/models/institution_spec.rb
153
+ - spec/routing/institution_management_routes_spec.rb
154
+ - spec/spec_helper.rb
155
+ - spec/support/Gemfile
156
+ - spec/support/app/models/sample.rb
157
+ - spec/support/app/models/solr_document.rb
158
+ - spec/support/config/initializers/hydra_config.rb
159
+ - spec/support/lib/generators/test_app_generator.rb
160
+ - spec/support/lib/tasks/rspec.rake
161
+ has_rdoc: