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 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.
@@ -2,58 +2,60 @@ require "ohio_state_person/version"
2
2
 
3
3
  module OhioStatePerson
4
4
 
5
- module ModelAdditions
6
- def is_a_buckeye
7
- extend ClassMethods
8
- include InstanceMethods
9
-
10
- validates_uniqueness_of :name_n
11
- validates_format_of :name_n, :with => /\A[a-z]([a-z-]*[a-z])?\.[1-9]\d*\z/, :message => 'must be in format: name.#'
12
-
13
- validates_uniqueness_of :emplid
14
- validates_format_of :emplid, :with => /\A\d{8,9}\z/, :message => 'must be 8 or 9 digits'
15
-
16
- before_validation :set_id, :on => :create
17
- validate :id_is_emplid
18
- end
19
- end
20
-
21
- module ClassMethods
22
- def search(q)
23
- q.strip! if q
24
- case q
25
- when /\A\d+\z/
26
- where(:emplid => q)
27
- when /\A\D+\.\d+\z/
28
- where(:name_n => q)
29
- when /(\S+),\s*(\S+)/
30
- where('last_name LIKE ? AND first_name LIKE ?', $1, "#{$2}%")
31
- when /(\S+)\s+(\S+)/
32
- where('first_name LIKE ? AND last_name LIKE ?', $1, "#{$2}%")
33
- when /\S/
34
- where('last_name LIKE ?', "#{q}%")
35
- else
36
- where('1=2')
37
- end
38
- end
39
- end
40
-
41
- module InstanceMethods
42
- def email
43
- name_n.present? ? "#{name_n}@osu.edu" : ''
44
- end
45
-
46
- protected
47
- def set_id
48
- self.id = self.emplid.to_i
49
- end
50
-
51
- def id_is_emplid
52
- unless self.id == self.emplid.to_i
53
- errors.add(:id, 'must be the same as the emplid')
54
- end
55
- end
56
- end
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
 
@@ -1,3 +1,3 @@
1
1
  module OhioStatePerson
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
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
- - 2
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: 2011-09-21 00:00:00 Z
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.10
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