trogdir_models 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 544ab6bcbbe2b0def5475ea269782fc4d774d6f4
4
+ data.tar.gz: 9c6d1ccb7ce16b62426c2e10c49b2d0d268fd4ed
5
+ SHA512:
6
+ metadata.gz: 2ef7fd02dd8257dbac93ecef8b123f6cb3de96e2eeb28b4fb5edcfec4e3e1152c0cee988b8e8ded8cd5b0d24f94e0004f05aada87a85f59c5b5d36e0c457669c
7
+ data.tar.gz: 3b0895645d120ce3de7bee1de8d22ff5b112b209ff45da2ee26c108ddede90ea6bcb27b26ad3fe1e6fed7be739b05136b53243696ccfa824371047f946e1ef27
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 by Biola University
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ Trogdir Models [![Build Status](https://travis-ci.org/biola/trogdir-models.png)](https://travis-ci.org/biola/trogdir-models)
2
+ ==============
3
+ A set of shared [Mongoid](http://mongoid.org) models for the Trogdir directory project.
@@ -0,0 +1,21 @@
1
+ class Address
2
+ include Mongoid::Document
3
+ include Mongoid::History::Trackable
4
+
5
+ TYPES = [:home]
6
+
7
+ embedded_in :person
8
+
9
+ field :type, type: Symbol
10
+ field :street_1, type: String
11
+ field :street_2, type: String
12
+ field :city, type: String
13
+ field :state, type: String
14
+ field :zip, type: String
15
+ field :country, type: String
16
+
17
+ validates :type, presence: true, inclusion: { in: Address::TYPES }
18
+ validates :street_1, presence: true
19
+
20
+ track_history track_create: true, track_destroy: true
21
+ end
@@ -0,0 +1,7 @@
1
+ class ChangeSync
2
+ include Mongoid::Document
3
+
4
+ belongs_to :changeset
5
+ belongs_to :syncinator
6
+ embeds_many :sync_logs
7
+ end
@@ -0,0 +1,7 @@
1
+ class Changeset
2
+ include Mongoid::History::Tracker
3
+
4
+ has_many :change_syncs
5
+
6
+ alias :person :trackable_root
7
+ end
@@ -0,0 +1,11 @@
1
+ module Employee
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ field :department, type: String
6
+ field :title, type: String
7
+ field :employee_type, type: Symbol
8
+ field :full_time, type: Boolean
9
+ field :pay_type, type: Symbol
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Student
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ # On-Campus Residence
6
+ field :residence, type: String
7
+ field :floor, type: Integer
8
+ field :wing, type: String
9
+
10
+ # Academic
11
+ field :majors, type: Array
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ class Email
2
+ include Mongoid::Document
3
+ include Mongoid::History::Trackable
4
+
5
+ embedded_in :person
6
+
7
+ TYPES = [:university, :personal]
8
+
9
+ field :type, type: Symbol
10
+ field :address, type: String
11
+ field :primary, type: Boolean
12
+
13
+ validates :type, presence: true, inclusion: { in: Email::TYPES }
14
+ validates :address, presence: true, email: true
15
+
16
+ track_history track_create: true, track_destroy: true
17
+ end
@@ -0,0 +1,25 @@
1
+ class ID
2
+ include Mongoid::Document
3
+ include Mongoid::History::Trackable
4
+
5
+ TYPES = [:biola_id, :netid, :banner, :google_apps]
6
+ DEFAULT_TYPE = :netid
7
+
8
+ embedded_in :person
9
+ field :type, type: Symbol
10
+ field :identifier
11
+
12
+ validates :type, presence: true, inclusion: { in: ID::TYPES }
13
+ validates :identifier, presence: true
14
+ validate do |id|
15
+ if Person.where(:id.ne => id.person.try(:id), 'ids.identifier' => id.identifier, 'ids.type' => id.type).count > 0
16
+ id.errors.add :identifier, 'must be unique'
17
+ end
18
+ end
19
+
20
+ track_history track_create: true, track_destroy: true
21
+
22
+ def to_s
23
+ identifier
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ class Person
2
+ include Mongoid::Document
3
+ include Mongoid::History::Trackable
4
+ include Student
5
+ include Employee
6
+
7
+ embeds_many :ids, class_name: 'ID'
8
+ embeds_many :emails
9
+ embeds_many :photos
10
+ embeds_many :phones
11
+ embeds_many :addresses
12
+ has_many :changesets, as: :changeable
13
+
14
+ # Names
15
+ field :first_name, type: String
16
+ field :preferred_name, type: String
17
+ field :middle_name, type: String
18
+ field :last_name, type: String
19
+ field :display_name, type: String
20
+
21
+ # Demographic
22
+ field :gender, type: Symbol
23
+ field :partial_ssn, type: String
24
+ field :birth_date, type: Date
25
+
26
+ # Groups and permissions
27
+ field :entitlements, type: Array
28
+ field :affiliations, type: Array
29
+
30
+ # Options
31
+ field :privacy, type: Boolean
32
+ field :enabled, type: Boolean # TODO: figure out if tihs is necessary
33
+
34
+ validates :first_name, :last_name, presence: true
35
+
36
+ track_history track_create: true
37
+
38
+ def email
39
+ emails.where(primary: true).first
40
+ end
41
+
42
+ delegate :address, to: :email, prefix: true, allow_nil: true
43
+ end
@@ -0,0 +1,17 @@
1
+ class Phone
2
+ include Mongoid::Document
3
+ include Mongoid::History::Trackable
4
+
5
+ TYPES = [:office, :home, :cell]
6
+
7
+ embedded_in :person
8
+
9
+ field :type, type: Symbol
10
+ field :number, type: String
11
+ field :primary, type: Boolean
12
+
13
+ validates :type, presence: true, inclusion: { in: Phone::TYPES }
14
+ validates :number, presence: true
15
+
16
+ track_history track_create: true, track_destroy: true
17
+ end
@@ -0,0 +1,19 @@
1
+ class Photo
2
+ include Mongoid::Document
3
+ include Mongoid::History::Trackable
4
+
5
+ TYPES = [:id_card]
6
+
7
+ embedded_in :person
8
+
9
+ field :type, type: Symbol
10
+ field :url, type: String
11
+ field :height, type: Integer
12
+ field :width, type: Integer
13
+
14
+ validates :type, presence: true, inclusion: { in: Photo::TYPES }
15
+ validates :url, :height, :width, presence: true
16
+ validates :url, absolute_uri: true
17
+
18
+ track_history track_create: true, track_destroy: true
19
+ end
@@ -0,0 +1,13 @@
1
+ class SyncLog
2
+ include Mongoid::Document
3
+
4
+ belongs_to :change_sync
5
+
6
+ field :started_at, type: Time
7
+ field :errored_at, type: Time
8
+ field :succeeded_at, type: Time
9
+ field :action, type: Symbol
10
+ field :message, type: String
11
+
12
+ validates :started_at, presence: true
13
+ end
@@ -0,0 +1,41 @@
1
+ require 'securerandom'
2
+
3
+ class Syncinator
4
+ include Mongoid::Document
5
+
6
+ FIXNUM_MAX = (2**(0.size * 8 -2) -1)
7
+
8
+ has_many :change_syncs
9
+
10
+ field :name, type: String
11
+ field :slug, type: String
12
+ field :access_id, type: Integer
13
+ field :secret_key, type: String
14
+ field :queue_changes, type: Boolean
15
+ field :active, type: Boolean
16
+
17
+ attr_readonly :slug, :access_id, :secret_key
18
+
19
+ validates :name, :slug, :access_id, :secret_key, presence: true
20
+ validates :name, :slug, :access_id, :secret_key, uniqueness: true
21
+
22
+ before_validation :set_default_slug, :set_access_id, :set_secret_key, on: :create
23
+
24
+ def to_s
25
+ name
26
+ end
27
+
28
+ private
29
+
30
+ def set_default_slug
31
+ self.slug ||= name.parameterize
32
+ end
33
+
34
+ def set_access_id
35
+ self.access_id = rand(FIXNUM_MAX)
36
+ end
37
+
38
+ def set_secret_key
39
+ self.secret_key = SecureRandom.hex(64)
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ require 'uri'
2
+
3
+ class AbsoluteUriValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ begin
6
+ unless URI.parse(value).absolute?
7
+ record.errors[attribute] << 'is not an absolute URL'
8
+ end
9
+ rescue URI::InvalidURIError => error
10
+ record.errors[attribute] << 'is not a valid URL'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ # From: https://gist.github.com/jcf/1188367
2
+
3
+ require 'mail'
4
+
5
+ class EmailValidator < ActiveModel::EachValidator
6
+ attr_reader :record, :attribute, :value, :email, :tree
7
+
8
+ def validate_each(record, attribute, value)
9
+ @record, @attribute, @value = record, attribute, value
10
+
11
+ @email = Mail::Address.new(value)
12
+ @tree = email.__send__(:tree)
13
+
14
+ add_error unless valid?
15
+ rescue Mail::Field::ParseError
16
+ add_error
17
+ end
18
+
19
+ private
20
+
21
+ def valid?
22
+ !!(domain_and_address_present? && domain_has_more_than_one_atom?)
23
+ end
24
+
25
+ def domain_and_address_present?
26
+ email.domain && email.address == value
27
+ end
28
+
29
+ def domain_has_more_than_one_atom?
30
+ tree.domain.dot_atom_text.elements.length > 1
31
+ end
32
+
33
+ def add_error
34
+ if message = options[:message]
35
+ record.errors[attribute] << message
36
+ else
37
+ record.errors.add(attribute, :invalid)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,31 @@
1
+ module TrogdirModels
2
+ # For sharing factories with apps that use the gem
3
+ def self.load_factories
4
+ FactoryGirl.definition_file_paths += [File.expand_path('../../spec/factories', __FILE__)]
5
+ end
6
+ end
7
+
8
+ # Models
9
+ require 'mongoid'
10
+ autoload :Person, 'trogdir/person'
11
+ autoload :ID, 'trogdir/id'
12
+ autoload :Email, 'trogdir/email'
13
+ autoload :Photo, 'trogdir/photo'
14
+ autoload :Phone, 'trogdir/phone'
15
+ autoload :Address, 'trogdir/address'
16
+ autoload :Changeset, 'trogdir/changeset'
17
+ autoload :Syncinator, 'trogdir/syncinator'
18
+ autoload :ChangeSync, 'trogdir/change_sync'
19
+ autoload :SyncLog, 'trogdir/sync_log'
20
+
21
+ # Concerns
22
+ require 'active_support/concern'
23
+ autoload :Student, 'trogdir/concerns/student'
24
+ autoload :Employee, 'trogdir/concerns/employee'
25
+
26
+ require 'active_model/validations'
27
+ autoload :EmailValidator, 'trogdir/validators/email_validator'
28
+ autoload :AbsoluteUriValidator, 'trogdir/validators/absolute_uri_validator'
29
+
30
+ require 'mongoid-history'
31
+ Mongoid::History.tracker_class_name = :changeset
@@ -0,0 +1,3 @@
1
+ module TrogdirModels
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,10 @@
1
+ FactoryGirl.define do
2
+ factory :address do
3
+ type :home
4
+ street_1 { Faker::Address.street_address }
5
+ city { Faker::Address.city }
6
+ state { Faker::Address.state_abbr }
7
+ zip { Faker::Address.zip }
8
+ country { Faker::Address.country }
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ FactoryGirl.define do
2
+ factory :email do
3
+ type :university
4
+ address { Faker::Internet.email }
5
+ primary false
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ FactoryGirl.define do
2
+ factory :id, class: ID do
3
+ type { :netid }
4
+ person
5
+ identifier { Faker::Internet.user_name }
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ FactoryGirl.define do
2
+ factory :person do
3
+ first_name { Faker::Name.first_name }
4
+ preferred_name { first_name }
5
+ last_name { Faker::Name.last_name }
6
+ display_name { "#{first_name} #{last_name}"}
7
+ gender { [:male, :female].sample }
8
+ partial_ssn { 1234 }
9
+ birth_date { 20.years.ago }
10
+ entitlements []
11
+ affiliations []
12
+ privacy false
13
+ enabled true
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :phone do
3
+ type :office
4
+ number { Faker::PhoneNumber.phone_number }
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ FactoryGirl.define do
2
+ factory :photo do
3
+ type :id_card
4
+ url { Faker::Internet.url }
5
+ height 100
6
+ width 100
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ FactoryGirl.define do
2
+ factory :syncinator do
3
+ name { Faker::Company.catch_phrase }
4
+ queue_changes true
5
+ active true
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trogdir_models
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Crownoble
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongoid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mongoid-history
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_girl
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.14'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.14'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mongoid-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.11'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.11'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.9'
125
+ description: A shared set of Mongoid models for the Trogdir directory project
126
+ email: adam.crownoble@biola.edu
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - MIT-LICENSE
132
+ - README.md
133
+ - lib/trogdir/address.rb
134
+ - lib/trogdir/change_sync.rb
135
+ - lib/trogdir/changeset.rb
136
+ - lib/trogdir/concerns/employee.rb
137
+ - lib/trogdir/concerns/student.rb
138
+ - lib/trogdir/email.rb
139
+ - lib/trogdir/id.rb
140
+ - lib/trogdir/person.rb
141
+ - lib/trogdir/phone.rb
142
+ - lib/trogdir/photo.rb
143
+ - lib/trogdir/sync_log.rb
144
+ - lib/trogdir/syncinator.rb
145
+ - lib/trogdir/validators/absolute_uri_validator.rb
146
+ - lib/trogdir/validators/email_validator.rb
147
+ - lib/trogdir_models.rb
148
+ - lib/trogdir_models/version.rb
149
+ - spec/factories/address_factory.rb
150
+ - spec/factories/email_factory.rb
151
+ - spec/factories/id_factory.rb
152
+ - spec/factories/person_factory.rb
153
+ - spec/factories/phone_factory.rb
154
+ - spec/factories/photo_factory.rb
155
+ - spec/factories/syncinator_factory.rb
156
+ homepage: https://github.com/biola/trogdir-models
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.2.2
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Shared models for the Trogdir directory
180
+ test_files: []