nameable_record 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,11 +8,38 @@ module NameableRecord::ActiveRecordExtensions
8
8
 
9
9
  def has_name( *args )
10
10
  args.each do |attr|
11
+
11
12
  composed_of attr, :class_name => "NameableRecord::Name", :mapping => [["#{attr}_last", "last"],
12
13
  ["#{attr}_first", "first"],
13
14
  ["#{attr}_prefix", "prefix"],
14
15
  ["#{attr}_middle", "middle"],
15
16
  ["#{attr}_suffix", "suffix"]]
17
+
18
+ define_method "conversational_#{attr}" do
19
+ [
20
+ send( "#{attr}_prefix" ),
21
+ send( "#{attr}_first" ),
22
+ send( "#{attr}_middle" ),
23
+ send( "#{attr}_last" ),
24
+ send( "#{attr}_suffix" )
25
+ ].reject( &:blank? ).
26
+ join( ' ' )
27
+ end
28
+
29
+ define_method "sortable_#{attr}" do
30
+ [
31
+ send( "#{attr}_last" ),
32
+ [
33
+ send( "#{attr}_prefix" ),
34
+ send( "#{attr}_first" ),
35
+ send( "#{attr}_middle" ),
36
+ send( "#{attr}_suffix" )
37
+ ].reject( &:blank? ).
38
+ join( ' ' )
39
+ ].reject( &:blank? ).
40
+ join( ', ' )
41
+ end
42
+
16
43
  end
17
44
  end
18
45
 
@@ -36,7 +36,11 @@ module NameableRecord
36
36
  # %s - suffix
37
37
  #
38
38
  def to_s( pattern='%l, %f' )
39
- pattern = PREDEFINED_PATTERNS[pattern] if pattern.is_a?( Symbol )
39
+ if pattern.is_a?( Symbol )
40
+ return conversational if pattern == :conversational
41
+ return sortable if pattern == :sortable
42
+ pattern = PREDEFINED_PATTERNS[pattern]
43
+ end
40
44
 
41
45
  PATTERN_MAP.inject( pattern ) do |name, mapping|
42
46
  name = name.gsub( mapping.first,
@@ -44,6 +48,35 @@ module NameableRecord
44
48
  end
45
49
  end
46
50
 
51
+ # Returns the name in a conversational format.
52
+ #
53
+ def conversational
54
+ [
55
+ prefix,
56
+ first,
57
+ middle,
58
+ last,
59
+ suffix
60
+ ].reject( &:blank? ).
61
+ join( ' ' )
62
+ end
63
+
64
+ # Returns the name in a sortable format.
65
+ #
66
+ def sortable
67
+ [
68
+ last,
69
+ [
70
+ prefix,
71
+ first,
72
+ middle,
73
+ suffix
74
+ ].reject( &:blank? ).
75
+ join( ' ' )
76
+ ].reject( &:blank? ).
77
+ join( ', ' )
78
+ end
79
+
47
80
  PREDEFINED_PATTERNS = {
48
81
  :full => "%l, %f %s",
49
82
  :full_with_middle => "%l, %f %m %s",
@@ -1,4 +1,3 @@
1
1
  module NameableRecord
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
4
-
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nameable_record
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Harrelson
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-25 00:00:00 -06:00
19
- default_executable:
18
+ date: 2012-06-11 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: gem-dandy
@@ -99,7 +98,6 @@ files:
99
98
  - spec/lib/nameable_record_spec.rb
100
99
  - spec/rails_spec_helper.rb
101
100
  - spec/spec_helper.rb
102
- has_rdoc: true
103
101
  homepage: https://github.com/midas/nameable_record
104
102
  licenses: []
105
103
 
@@ -129,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
127
  requirements: []
130
128
 
131
129
  rubyforge_project: nameable_record
132
- rubygems_version: 1.6.0
130
+ rubygems_version: 1.8.10
133
131
  signing_key:
134
132
  specification_version: 3
135
133
  summary: Abstracts the ActiveRecord composed_of pattern for names.