ucb_orgs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e4844c4ae70974f3946582344232605c9dc93f00
4
+ data.tar.gz: b0e1d755652ff7172cd099a43e1053f2021a9f6b
5
+ SHA512:
6
+ metadata.gz: 1ef72e2169fdfacd5dc7cb455477a6f3846991c02fe0eb077aadf52c6268398e0175835812f7481788f3a70ed0b6d6b783d36f90958b8f0ff7b3ad224f184300
7
+ data.tar.gz: ca8feca9fab74d14d98f140444fb6d4804b847352a20ad148136b625c0d5b178e5429765cb04e8538989d0b9f31568758117818fa195729f4e553101ff942606
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at darinwilson@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ucb_orgs.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Darin Wilson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # UcbOrgs
2
+
3
+ The ucb_orgs gem is a Rails plugin that provides a model and backing database table for UC Berkeley org units.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ucb_orgs'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ucb_orgs
20
+
21
+ Once you've installed the gem, you'll need to setup the database table and install the org unit data.
22
+
23
+ To add the migrations to your app, run:
24
+
25
+ ```
26
+ rake ucb_orgs:install
27
+ ```
28
+
29
+ Then run `rake db:migrate` as usual.
30
+
31
+ To load the most recent org unit data, run:
32
+
33
+ ```
34
+ rake ucb_orgs:update
35
+ ```
36
+
37
+ ## Updating
38
+
39
+ To update your local database with the most recent version of the org chart, run:
40
+
41
+ ```
42
+ rake ucb_orgs:update
43
+ ```
44
+
45
+ This can safely be run as often as needed.
46
+
47
+ ## Data Model
48
+
49
+ Org units are hierarchical, and each `OrgUnit` record contains the org unit's code, its level in the hierarchy, and the codes of the org units above it.
50
+
51
+ For example:
52
+
53
+ Code | Name | Level | Level_2 | Level_3 | Level_4 | Level_5 | Level_6
54
+ ----- | ----- | ----- | ----- | ----- | ----- | ----- | -----
55
+ AZRAD | Shared Services Research Admin | 5 |CAMSU | VCBAS | AZCSS | AZRAD
56
+
57
+ This org unit is at level 5, so the record contains the org units for levels 2, 3, and 4 above.
58
+
59
+ There is exactly one org unit at level one, UCBKL, which represents the entire campus.
60
+
61
+ ## Usage
62
+
63
+ The org unit model will be available in your code as `UcbOrgs::OrgUnit.` This is an ordinary ActiveRecord model, so all of the usual methods are available to you.
64
+
65
+ If you'd like to add or override methods in the model, create `org_unit.rb` in your `models` directory and set it up like this:
66
+
67
+ ```ruby
68
+ class OrgUnit < ApplicationRecord
69
+ include UcbOrgs::Concerns::OrgUnit
70
+
71
+ # add customized behavior here
72
+
73
+ end
74
+ ```
75
+
76
+ You can name it anything you like, just make sure you have the `include` statement shown above.
77
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ucb_orgs"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,45 @@
1
+ module UcbOrgs
2
+ module Concerns
3
+ module OrgUnit
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+
8
+ def load_from_csv(csv_filename)
9
+ CSV.foreach(csv_filename, headers: true) do |row|
10
+ org_unit = UcbOrgs::OrgUnit.find_or_create_by(code: row['CODE'])
11
+ org_unit.update_attributes(
12
+ name: row['NAME'],
13
+ level: row['LEVEL'].to_i,
14
+ level_2: row['LEVEL_2'],
15
+ level_3: row['LEVEL_3'],
16
+ level_4: row['LEVEL_4'],
17
+ level_5: row['LEVEL_5'],
18
+ level_6: row['LEVEL_6']
19
+ )
20
+ end
21
+ end
22
+
23
+ # Returns an Array of org codes consisting of the given org and all child orgs. "org" can also
24
+ # be a list of orgs. The result will have all duplicate org codes filtered out.
25
+ def org_with_children(org)
26
+ orgs = Array(org)
27
+ return [] if orgs.empty?
28
+ orgs_str = orgs.reduce("(") { |res,org| res + "'#{org}'," }.chop + ")"
29
+ # TODO we can clean this up with "or" queries in Rails 5
30
+ UcbOrgs::OrgUnit
31
+ .where(
32
+ "code IN #{orgs_str}" +
33
+ "OR level_2 IN #{orgs_str}" +
34
+ "OR level_3 IN #{orgs_str}" +
35
+ "OR level_4 IN #{orgs_str}" +
36
+ "OR level_5 IN #{orgs_str}" +
37
+ "OR level_6 IN #{orgs_str}"
38
+ )
39
+ .uniq
40
+ .pluck(:code)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ class CreateOrgUnits < ActiveRecord::Migration[5.1]
2
+ def up
3
+ create_table :org_units do |t|
4
+ t.string :code
5
+ t.string :name
6
+ t.integer :level
7
+ t.string :level_2
8
+ t.string :level_3
9
+ t.string :level_4
10
+ t.string :level_5
11
+ t.string :level_6
12
+
13
+ t.timestamps
14
+ end
15
+
16
+ add_index :org_units, :code, unique: true
17
+
18
+ end
19
+
20
+ def down
21
+ drop_table :org_units
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ require "csv"
2
+
3
+ module UcbOrgs
4
+
5
+ class OrgUnit < ActiveRecord::Base
6
+ include UcbOrgs::Concerns::OrgUnit
7
+
8
+ # Don't add anything else here - any other functionality should be added to
9
+ # UcbOrgs::Concerns::OrgUnit. This will make it easier for host apps to
10
+ # customize the class as needed. See:
11
+ # http://edgeguides.rubyonrails.org/engines.html#implementing-decorator-pattern-using-activesupport-concern
12
+ end
13
+
14
+ end
@@ -0,0 +1,5 @@
1
+ class UcbOrgs::Railtie < Rails::Railtie
2
+ rake_tasks do
3
+ load "ucb_orgs/tasks/ucb_orgs.rake"
4
+ end
5
+ end
@@ -0,0 +1,83 @@
1
+ module UcbOrgs
2
+ class SyncError < StandardError
3
+ attr_reader :item
4
+ attr_reader :source_error
5
+
6
+ def initialize(item, source_error)
7
+ @item = item
8
+ @source_error = source_error
9
+ end
10
+
11
+ def error_details
12
+ "#{source_error} : #{source_error.backtrace.first}"
13
+ end
14
+ end
15
+
16
+ # Syncs the org units to the LDAP tree
17
+ #
18
+ # Since ldap is definitive and we don't keep org history,
19
+ # delete any department row that wasn't found in ldap.
20
+ #
21
+ # Most of this code was lifted wholesale from APBears
22
+ class Syncer
23
+ attr_accessor :ldap_org_entries
24
+
25
+ class << self
26
+ def sync
27
+ UcbOrgs::Syncer.new.sync
28
+ end
29
+ end
30
+
31
+ def sync
32
+ UCB::LDAP::Org.root_node # force load of whole tree
33
+ self.ldap_org_entries = UCB::LDAP::Org.flattened_tree
34
+
35
+ sync_orgs
36
+ delete_not_found_in_ldap
37
+ end
38
+
39
+ private
40
+
41
+ def sync_orgs
42
+ ldap_org_entries.each_with_index do |ldap_org_entry, index|
43
+ begin
44
+ sync_org(ldap_org_entry, index)
45
+ rescue Exception => e
46
+ raise UcbOrgs::SyncError.new(ldap_org_entry.try(:code), e)
47
+ end
48
+ end
49
+ end
50
+
51
+ def delete_not_found_in_ldap
52
+ codes_found_in_ldap = ldap_org_entries.map(&:code)
53
+ codes_found_in_db = UcbOrgs::OrgUnit.all.map(&:code)
54
+ codes_to_delete = codes_found_in_db - codes_found_in_ldap
55
+
56
+ UcbOrgs::OrgUnit
57
+ .where(code: codes_to_delete)
58
+ .delete_all
59
+ end
60
+
61
+ def sync_org(ldap_org_entry, index)
62
+ UcbOrgs::OrgUnit.find_or_initialize_by(code: ldap_org_entry.code).tap do |dept|
63
+ dept.update_attributes!(
64
+ code: nb_to_string(ldap_org_entry.code),
65
+ name: nb_to_string(ldap_org_entry.name),
66
+ level: ldap_org_entry.level,
67
+ level_2: nb_to_string(ldap_org_entry.level_2_code),
68
+ level_3: nb_to_string(ldap_org_entry.level_3_code),
69
+ level_4: nb_to_string(ldap_org_entry.level_4_code),
70
+ level_5: nb_to_string(ldap_org_entry.level_5_code),
71
+ level_6: nb_to_string(ldap_org_entry.level_6_code)
72
+ )
73
+ end
74
+ end
75
+
76
+ # Net-Ldap returns Net::BER::BerIdentifiedString.
77
+ # Explicityly confert to String.
78
+ def nb_to_string(nb)
79
+ nb.nil? ? nil : nb.to_s
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,44 @@
1
+ require "ucb_orgs/syncer"
2
+
3
+ namespace :ucb_orgs do
4
+ desc "Installs migrations to the current project"
5
+ task :install do
6
+ puts "Checking for new migrations..."
7
+ migrations_to_add = new_migrations()
8
+ if migrations_to_add.empty?
9
+ puts "No new migrations"
10
+ else
11
+ migrations_to_add.each do |migration|
12
+ FileUtils.cp("#{migration_source_dir}/#{migration}", migration_target_dir)
13
+ end
14
+ puts "\nMigrations installed - ready to run \"rake db:migrate\""
15
+ end
16
+ end
17
+
18
+ desc "Downloads org unit source file, and loads it into the database"
19
+ task update: :environment do
20
+ puts "Updating org units..."
21
+ begin
22
+ UcbOrgs::Syncer.sync
23
+ puts "Done."
24
+ rescue Exception => e
25
+ msg = e.respond_to?(:error_details) ? e.error_details : e.to_s
26
+ puts "Unable to complete sync: #{msg}"
27
+ end
28
+ end
29
+
30
+ def migration_source_dir
31
+ "#{File.dirname(__FILE__)}/../migrations/"
32
+ end
33
+
34
+ def migration_target_dir
35
+ "#{Rails.root}/db/migrate"
36
+ end
37
+
38
+ def new_migrations
39
+ already_installed = Dir.entries(migration_target_dir)
40
+ Dir.entries(migration_source_dir).reject do |m|
41
+ already_installed.include?(m)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module UcbOrgs
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ucb_orgs.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "ucb_orgs/version"
2
+ require "ucb_orgs/concerns/org_unit"
3
+ require "ucb_orgs/org_unit"
4
+ require "ucb_orgs/railtie" if defined?(Rails)
5
+
6
+ module UcbOrgs
7
+ end
data/ucb_orgs.gemspec ADDED
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ucb_orgs/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ucb_orgs"
8
+ spec.version = UcbOrgs::VERSION
9
+ spec.authors = ["Darin Wilson"]
10
+ spec.email = ["darinwilson@gmail.com"]
11
+
12
+ spec.summary = %q{Rails extension that manages UCB org unit data}
13
+ spec.homepage = "https://github.com/ucb-ist-eas/ucb_orgs"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "ucb_ldap"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ucb_orgs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Darin Wilson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ucb_ldap
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - darinwilson@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/ucb_orgs.rb
87
+ - lib/ucb_orgs/concerns/org_unit.rb
88
+ - lib/ucb_orgs/migrations/20171211220534_create_org_units.rb
89
+ - lib/ucb_orgs/org_unit.rb
90
+ - lib/ucb_orgs/railtie.rb
91
+ - lib/ucb_orgs/syncer.rb
92
+ - lib/ucb_orgs/tasks/ucb_orgs.rake
93
+ - lib/ucb_orgs/version.rb
94
+ - ucb_orgs.gemspec
95
+ homepage: https://github.com/ucb-ist-eas/ucb_orgs
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Rails extension that manages UCB org unit data
119
+ test_files: []