hashdown 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/lib/hashdown/select_options.rb +22 -3
- data/lib/hashdown/version.rb +1 -1
- data/spec/lib/hashdown/select_options_spec.rb +6 -0
- data/spec/support/models.rb +4 -0
- metadata +12 -18
data/README.md
CHANGED
@@ -32,7 +32,7 @@ You can look up states via their abbreviations like so:
|
|
32
32
|
@colorado = State[:CO]
|
33
33
|
|
34
34
|
By default, calling the finder method with a token that does not exist in the
|
35
|
-
database will result in an ActiveRecord::
|
35
|
+
database will result in an ActiveRecord::RecordNotFound exception being thrown.
|
36
36
|
This can be overridden by adding a :default option to your finder declaration.
|
37
37
|
|
38
38
|
class PurchaseOrder < ActiveRecord::Base
|
@@ -74,11 +74,11 @@ implementation:
|
|
74
74
|
validates_uniqueness_of :abbreviation
|
75
75
|
|
76
76
|
def self.[](state_code)
|
77
|
-
|
77
|
+
where(abbreviation: state_code).first
|
78
78
|
end
|
79
79
|
|
80
80
|
def self.select_options
|
81
|
-
|
81
|
+
all.map{|s| [s.name, s.id] }
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
@@ -16,9 +16,12 @@ module Hashdown
|
|
16
16
|
options[:value] ||= args.shift || :id
|
17
17
|
|
18
18
|
scope = scoped
|
19
|
-
|
19
|
+
options[:is_sorted] = scope.arel.orders.any?
|
20
|
+
scope = select_options_scope_with_order(scope, options)
|
20
21
|
|
21
|
-
Hashdown.
|
22
|
+
cache_key = Hashdown.cache_key(:select_options, self.to_s, select_options_cache_key(options, scope))
|
23
|
+
|
24
|
+
Hashdown.cache.fetch(cache_key, :force => Hashdown.force_cache_miss?) do
|
22
25
|
if grouping = options[:group]
|
23
26
|
scope.all.group_by {|record| grouping.call(record) }.map do |group, records|
|
24
27
|
[group, map_records_to_select_options(records, options)]
|
@@ -39,7 +42,23 @@ module Hashdown
|
|
39
42
|
end
|
40
43
|
|
41
44
|
def map_records_to_select_options(records, options)
|
42
|
-
records.map{|record|
|
45
|
+
records = records.map { |record| select_option_for_record(record, options) }
|
46
|
+
options[:is_sorted] ? records : records.sort
|
47
|
+
end
|
48
|
+
|
49
|
+
def select_option_for_record(record, options)
|
50
|
+
[ record.send(options[:label]), record.send(options[:value]) ]
|
51
|
+
end
|
52
|
+
|
53
|
+
def select_options_scope_with_order(scope, options)
|
54
|
+
unless options[:is_sorted]
|
55
|
+
if columns.any? { |c| c.name == options[:label].to_s }
|
56
|
+
options[:is_sorted] = true
|
57
|
+
return scope.order(options[:label])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
scope
|
43
62
|
end
|
44
63
|
end
|
45
64
|
|
data/lib/hashdown/version.rb
CHANGED
@@ -15,6 +15,12 @@ describe Hashdown::SelectOptions do
|
|
15
15
|
State.select_options.map(&:first).should eq state_names
|
16
16
|
end
|
17
17
|
|
18
|
+
it 'allows use of methods for labels' do
|
19
|
+
State.select_options(:label => :label).map(&:first).should eq(
|
20
|
+
['AZ (Arizona)', 'CA (California)', 'CO (Colorado)', 'NY (New York)', 'TX (Texas)']
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
18
24
|
it 'respects order if specified' do
|
19
25
|
Currency.select_options.map(&:first).should eq ['Renminbi', 'Euro', 'Pound Sterling', 'US Dollar']
|
20
26
|
end
|
data/spec/support/models.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashdown
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70355080435780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70355080435780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: pry-nav
|
27
|
-
requirement: &
|
27
|
+
requirement: &70355080435160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70355080435160
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70355080434440 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70355080434440
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70355080433760 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70355080433760
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: sqlite3
|
60
|
-
requirement: &
|
60
|
+
requirement: &70355080421380 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70355080421380
|
69
69
|
description: Hashdown
|
70
70
|
email:
|
71
71
|
- rubysolo@gmail.com
|
@@ -103,18 +103,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
103
|
- - ! '>='
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
|
-
segments:
|
107
|
-
- 0
|
108
|
-
hash: -1096798538704753083
|
109
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
107
|
none: false
|
111
108
|
requirements:
|
112
109
|
- - ! '>='
|
113
110
|
- !ruby/object:Gem::Version
|
114
111
|
version: '0'
|
115
|
-
segments:
|
116
|
-
- 0
|
117
|
-
hash: -1096798538704753083
|
118
112
|
requirements: []
|
119
113
|
rubyforge_project:
|
120
114
|
rubygems_version: 1.8.11
|