ohio_state_person 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +33 -0
- data/lib/ohio_state_person.rb +54 -52
- data/lib/ohio_state_person/version.rb +1 -1
- metadata +5 -4
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# ohio_state_person
|
2
|
+
|
3
|
+
[Gem page](https://rubygems.org/gems/ohio_state_person)
|
4
|
+
|
5
|
+
|
6
|
+
## Description
|
7
|
+
|
8
|
+
This is an extraction of the common user model code in our Rails apps.
|
9
|
+
|
10
|
+
|
11
|
+
## Synopsis
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
class Student < ActiveRecord::Base
|
15
|
+
is_a_buckeye
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
|
20
|
+
## Features
|
21
|
+
|
22
|
+
It validates the format and uniqueness of ```name_n``` and ```emplid```.
|
23
|
+
|
24
|
+
It sets the ```id``` of new records to ```emplid.to_i```, and validates that the ```id``` is always ```emplid.to_i```.
|
25
|
+
|
26
|
+
It adds a class method: ```search```, which searches by ```emplid```, ```name_n```, ```last_name, first_name```, ```first_name last_name```, or just ```last_name```, depending on whether the search term looks like an emplid, name_n, etc.
|
27
|
+
|
28
|
+
It adds an instance method: ```email```, which is just ```"#{name_n}@osu.edu"```.
|
29
|
+
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
Just call ```is_a_buckeye``` from the class level in your model. This mixin expects ```emplid```, ```name_n```, ```first_name```, and ```last_name``` to be attributes of the model in question.
|
data/lib/ohio_state_person.rb
CHANGED
@@ -2,58 +2,60 @@ require "ohio_state_person/version"
|
|
2
2
|
|
3
3
|
module OhioStatePerson
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
5
|
+
module ModelAdditions
|
6
|
+
def is_a_buckeye
|
7
|
+
extend ClassMethods
|
8
|
+
include InstanceMethods
|
9
|
+
|
10
|
+
if column_names.include? 'name_n'
|
11
|
+
validates_uniqueness_of :name_n
|
12
|
+
validates_format_of :name_n, :with => /\A[a-z]([a-z-]*[a-z])?\.[1-9]\d*\z/, :message => 'must be in format: name.#'
|
13
|
+
end
|
14
|
+
|
15
|
+
validates_uniqueness_of :emplid
|
16
|
+
validates_format_of :emplid, :with => /\A\d{8,9}\z/, :message => 'must be 8 or 9 digits'
|
17
|
+
|
18
|
+
before_validation :set_id, :on => :create
|
19
|
+
validate :id_is_emplid
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def search(q)
|
25
|
+
q = q.to_s.strip
|
26
|
+
h = ActiveSupport::OrderedHash.new
|
27
|
+
h[/\A\d+\z/] = lambda { where(:emplid => q) }
|
28
|
+
h[/\A\D+\.\d+\z/] = lambda { where(:name_n => q) } if column_names.include? 'name_n'
|
29
|
+
h[/(\S+),\s*(\S+)/] = lambda { where('last_name LIKE ? AND first_name LIKE ?', $1, "#{$2}%") }
|
30
|
+
h[/(\S+)\s+(\S+)/] = lambda { where('first_name LIKE ? AND last_name LIKE ?', $1, "#{$2}%") }
|
31
|
+
h[/\S/] = lambda { where('last_name LIKE ?', "#{q}%") }
|
32
|
+
h[//] = lambda { where('1=2') }
|
33
|
+
|
34
|
+
h.each do |regex, where_clause|
|
35
|
+
return where_clause.call if q =~ regex
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module InstanceMethods
|
42
|
+
def email
|
43
|
+
return self[:email] if self.class.column_names.include? 'email'
|
44
|
+
return nil unless self.class.column_names.include? 'name_n'
|
45
|
+
name_n.present? ? "#{name_n}@osu.edu" : ''
|
46
|
+
end
|
47
|
+
|
48
|
+
protected
|
49
|
+
def set_id
|
50
|
+
self.id = self.emplid.to_i
|
51
|
+
end
|
52
|
+
|
53
|
+
def id_is_emplid
|
54
|
+
unless self.id == self.emplid.to_i
|
55
|
+
errors.add(:id, 'must be the same as the emplid')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
57
59
|
|
58
60
|
end
|
59
61
|
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mikegee
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-02-06 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: activerecord
|
@@ -44,6 +44,7 @@ extra_rdoc_files: []
|
|
44
44
|
files:
|
45
45
|
- .gitignore
|
46
46
|
- Gemfile
|
47
|
+
- README.md
|
47
48
|
- Rakefile
|
48
49
|
- lib/ohio_state_person.rb
|
49
50
|
- lib/ohio_state_person/version.rb
|
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
78
|
requirements: []
|
78
79
|
|
79
80
|
rubyforge_project: ohio_state_person
|
80
|
-
rubygems_version: 1.8.
|
81
|
+
rubygems_version: 1.8.15
|
81
82
|
signing_key:
|
82
83
|
specification_version: 3
|
83
84
|
summary: ActiveRecord mixin for people at Ohio State University
|