livelist-rails 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,8 @@ module Livelist
14
14
  :reference_criteria => options[:reference_criteria],
15
15
  :name => options[:name],
16
16
  :model_name => model_name,
17
+ :attribute => options[:attribute],
18
+ :key_name => options[:key_name],
17
19
  :slug => slug
18
20
  )
19
21
  end
@@ -25,6 +27,7 @@ module Livelist
25
27
  def filter(params, options = {})
26
28
  filters.relation(scoped, params, options)
27
29
  end
30
+
28
31
  end
29
32
  end
30
33
  end
@@ -1,31 +1,37 @@
1
1
  require 'active_record'
2
+ require 'active_support/inflector'
2
3
  require 'livelist/rails/filter_criteria'
3
4
 
4
5
  module Livelist
5
6
  module Rails
6
7
 
7
8
  class Filter
8
- DEFAULT_FILTER_OPTIONS = {
9
- :reference_criteria => nil
10
- }
9
+ DEFAULT_FILTER_OPTIONS = { :reference_criteria => nil }
11
10
 
12
11
  attr_accessor :slug,
13
12
  :name,
14
13
  :key_name,
15
14
  :model_name,
16
15
  :model_class,
16
+ :attribute,
17
17
  :join,
18
18
  :type,
19
+ :criterion_label,
19
20
  :criteria
20
21
 
21
22
  # slug should always be a symbol
22
23
  def initialize(options = {})
24
+ raise ArgumentError, 'slug option required' unless options[:slug]
25
+ raise ArgumentError, 'model_name option required' unless options[:model_name]
26
+
23
27
  @filter_collection = options[:filter_collection]
24
28
  @slug = options[:slug].to_sym
25
29
  @name = options[:name] || @slug.to_s.capitalize
26
30
  @model_name = options[:model_name]
27
31
  @type = options[:type] || initialize_type
28
32
  @key_name = options[:key_name] || default_key_name
33
+ @attribute = options[:attribute] || default_key_name
34
+ @criterion_label = options[:criterion_label]
29
35
  @criteria = FilterCriteria.new(
30
36
  :filter => self,
31
37
  :reference_criteria => options[:reference_criteria],
@@ -76,7 +82,7 @@ module Livelist
76
82
 
77
83
  def default_key_name
78
84
  case @type
79
- when :association then :id
85
+ when :association then @attribute || :id
80
86
  when :attribute then @slug
81
87
  end
82
88
  end
@@ -92,8 +98,12 @@ module Livelist
92
98
  @model_name.classify.constantize
93
99
  end
94
100
 
101
+ def filter_class
102
+ slug.to_s.classify.constantize
103
+ end
104
+
95
105
  def where(slug_params)
96
- { table_name => { @key_name => slug_params } }
106
+ { table_name => { @attribute => slug_params } }
97
107
  end
98
108
 
99
109
  def as_json(params)
@@ -30,7 +30,7 @@ module Livelist
30
30
  def default_reference_criteria
31
31
  case @filter.type
32
32
  when :attribute then @filter.model_class.select("distinct #{@filter.slug}")
33
- when :association then @filter.slug.to_s.classify.constantize.scoped
33
+ when :association then @filter.filter_class.scoped
34
34
  end
35
35
  end
36
36
 
@@ -9,14 +9,14 @@ module Livelist
9
9
  :count,
10
10
  :value,
11
11
  :type,
12
- :name_key
12
+ :label
13
13
 
14
14
  def initialize(options = {})
15
15
  @filter = options[:filter]
16
16
  @criteria = options[:criteria]
17
17
  @reference = options[:reference]
18
18
  @type = infer_type
19
- @name_key = options[:name_key] || infer_name_key
19
+ @label = @filter.criterion_label || infer_label
20
20
  @slug = infer_slug
21
21
  @name = infer_name
22
22
  end
@@ -48,7 +48,7 @@ module Livelist
48
48
  end
49
49
  end
50
50
 
51
- def infer_name_key
51
+ def infer_label
52
52
  case @type
53
53
  when :scalar then nil
54
54
  when :hash then :name
@@ -71,8 +71,8 @@ module Livelist
71
71
  def infer_name
72
72
  case @type
73
73
  when :scalar then @reference
74
- when :hash then @reference[@name_key]
75
- when :model then @reference.send(@name_key)
74
+ when :hash then @reference[@label]
75
+ when :model then @reference.send(@label)
76
76
  end
77
77
  end
78
78
  end
@@ -1,6 +1,6 @@
1
1
  module Livelist
2
2
  module Rails
3
- VERSION = '0.0.14'
3
+ VERSION = '0.0.15'
4
4
  LIVELIST_VERSION = '0.0.9'
5
5
  MUSTACHE_VERSION = '0.4.2'
6
6
  UNDERSCORE_VERSION = '1.4.2'
@@ -2,10 +2,26 @@ require 'spec_helper.rb'
2
2
  require File.expand_path('./lib/livelist/rails/filter_criterion.rb')
3
3
 
4
4
  describe Livelist::Rails::FilterCriterion do
5
- subject { FilterCritereon.new }
5
+
6
+ let(:filter) { double(:filter, :criterion_label => nil) }
7
+ let(:criteria) { double(:criteria, :slug => :name) }
8
+ let(:reference) { {} }
9
+
10
+ subject do
11
+ Livelist::Rails::FilterCriterion.new(
12
+ :filter => filter,
13
+ :criteria => criteria,
14
+ :reference => reference
15
+ )
16
+ end
6
17
 
7
18
  context :initialize do
19
+ its(:label) { should_not be_nil }
8
20
 
21
+ context 'when criterion_label is set on the filter' do
22
+ let(:filter) { double(:filter, :criterion_label => :label) }
23
+ its(:label) { should == :label }
24
+ end
9
25
  end
10
26
 
11
27
  context :selected? do
@@ -2,10 +2,36 @@ require 'spec_helper.rb'
2
2
  require File.expand_path('./lib/livelist/rails/filter.rb')
3
3
 
4
4
  describe Livelist::Rails::Filter do
5
- subject { Filter.new }
5
+
6
+ class MyModel; end
7
+
8
+ let(:options) do
9
+ {
10
+ :slug => :my_filter,
11
+ :model_name => :my_model
12
+ }
13
+ end
14
+ subject { Livelist::Rails::Filter.new(options) }
6
15
 
7
16
  context :initialize do
8
17
 
18
+ its(:criterion_label) { should be_nil }
19
+
20
+ context 'when criterion_label option is set' do
21
+ before { options[:criterion_label] = :label }
22
+ its(:criterion_label) { should == :label }
23
+ end
24
+
25
+ context 'when slug option is not set' do
26
+ subject { Livelist::Rails::Filter.new }
27
+ it { -> { subject }.should raise_error ArgumentError, 'slug option required' }
28
+ end
29
+
30
+ context 'when model_name option is not set' do
31
+ subject { Livelist::Rails::Filter.new(:slug => :my_filter) }
32
+ it { -> { subject }.should raise_error ArgumentError, 'model_name option required' }
33
+ end
34
+
9
35
  end
10
36
 
11
37
  context :group_by do
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,7 @@
4
4
  # loaded once.
5
5
 
6
6
  require 'simplecov'
7
+
7
8
  SimpleCov.start
8
9
 
9
10
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: livelist-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-30 00:00:00.000000000 Z
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -170,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  version: '0'
171
171
  requirements: []
172
172
  rubyforge_project: livelist-rails
173
- rubygems_version: 1.8.24
173
+ rubygems_version: 1.8.25
174
174
  signing_key:
175
175
  specification_version: 3
176
176
  summary: A Rails Engine/Extension Incorporating Livelist.js