hashdown 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,16 @@
1
1
  module Hashdown
2
2
  module SelectOptions
3
- def select_options(*args)
4
- unless self.respond_to?(:map_records_to_select_options)
5
- self.extend ClassMethods
6
- self.send(:include, InstanceMethods)
3
+ def selectable(*args)
4
+ unless respond_to?(:select_options)
5
+ extend ClassMethods
6
+ include InstanceMethods
7
7
 
8
8
  after_save :clear_select_options_cache
9
9
  after_destroy :clear_select_options_cache
10
+
11
+ class << self
12
+ attr_accessor :select_options_options
13
+ end
10
14
  end
11
15
 
12
16
  options = args.pop if args.last.is_a?(Hash)
@@ -15,24 +19,35 @@ module Hashdown
15
19
  options[:label] ||= args.shift || :name
16
20
  options[:value] ||= args.shift || :id
17
21
 
18
- scope = scoped
19
- options[:is_sorted] = scope.arel.orders.any?
20
- scope = select_options_scope_with_order(scope, options)
22
+ self.select_options_options = options
23
+ end
24
+
25
+ module ClassMethods
26
+ def select_options(*args)
27
+ options = args.pop if args.last.is_a?(Hash)
28
+ options ||= {}
21
29
 
22
- cache_key = Hashdown.cache_key(:select_options, self.to_s, select_options_cache_key(options, scope))
30
+ options[:label] ||= args.shift if args.any?
31
+ options[:value] ||= args.shift if args.any?
32
+ options.reverse_merge!(select_options_options)
23
33
 
24
- Hashdown.cache.fetch(cache_key, :force => Hashdown.force_cache_miss?) do
25
- if grouping = options[:group]
26
- scope.all.group_by {|record| grouping.call(record) }.map do |group, records|
27
- [group, map_records_to_select_options(records, options)]
34
+ scope = scoped
35
+ options[:is_sorted] = scope.arel.orders.any?
36
+ scope = select_options_scope_with_order(scope, options)
37
+
38
+ cache_key = Hashdown.cache_key(:select_options, self.to_s, select_options_cache_key(options, scope))
39
+
40
+ Hashdown.cache.fetch(cache_key, :force => Hashdown.force_cache_miss?) do
41
+ if grouping = options[:group]
42
+ scope.all.group_by {|record| grouping.call(record) }.map do |group, records|
43
+ [group, map_records_to_select_options(records, options)]
44
+ end
45
+ else
46
+ map_records_to_select_options(scope.all, options)
28
47
  end
29
- else
30
- map_records_to_select_options(scope.all, options)
31
48
  end
32
49
  end
33
- end
34
50
 
35
- module ClassMethods
36
51
  private
37
52
 
38
53
  def select_options_cache_key(options, scope)
@@ -1,3 +1,3 @@
1
1
  module Hashdown
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Hashdown::SelectOptions do
4
4
  let(:state_names) { %w(Arizona California Colorado New\ York Texas) }
5
5
 
6
- it 'adds #select_options to ActiveRecord classes' do
6
+ it 'adds #select_options to ActiveRecord classes declared selectable' do
7
7
  State.should respond_to(:select_options)
8
8
  end
9
9
 
@@ -16,7 +16,7 @@ describe Hashdown::SelectOptions do
16
16
  end
17
17
 
18
18
  it 'allows use of methods for labels' do
19
- State.select_options(:label => :label).map(&:first).should eq(
19
+ State.select_options(:label).map(&:first).should eq(
20
20
  ['AZ (Arizona)', 'CA (California)', 'CO (Colorado)', 'NY (New York)', 'TX (Texas)']
21
21
  )
22
22
  end
@@ -1,6 +1,7 @@
1
1
  class State < ActiveRecord::Base
2
2
  scope :starting_with_c, where("name like 'C%'")
3
3
  finder :abbreviation
4
+ selectable
4
5
 
5
6
  def label
6
7
  "#{ abbreviation } (#{ name })"
@@ -20,4 +21,5 @@ end
20
21
 
21
22
  class Currency < ActiveRecord::Base
22
23
  default_scope order(:code)
24
+ selectable
23
25
  end
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.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70355080435780 !ruby/object:Gem::Requirement
16
+ requirement: &70280660899280 !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: *70355080435780
24
+ version_requirements: *70280660899280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: pry-nav
27
- requirement: &70355080435160 !ruby/object:Gem::Requirement
27
+ requirement: &70280660898640 !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: *70355080435160
35
+ version_requirements: *70280660898640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70355080434440 !ruby/object:Gem::Requirement
38
+ requirement: &70280660897960 !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: *70355080434440
46
+ version_requirements: *70280660897960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70355080433760 !ruby/object:Gem::Requirement
49
+ requirement: &70280660889020 !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: *70355080433760
57
+ version_requirements: *70280660889020
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sqlite3
60
- requirement: &70355080421380 !ruby/object:Gem::Requirement
60
+ requirement: &70280660888540 !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: *70355080421380
68
+ version_requirements: *70280660888540
69
69
  description: Hashdown
70
70
  email:
71
71
  - rubysolo@gmail.com