kashiwamochi 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/kashiwamochi/action_view.rb +13 -7
- data/lib/kashiwamochi/query.rb +16 -25
- data/lib/kashiwamochi/sort.rb +2 -1
- data/lib/kashiwamochi/version.rb +1 -1
- data/spec/helpers/action_view_spec.rb +58 -8
- data/spec/kashiwamochi/query_spec.rb +14 -19
- metadata +4 -4
@@ -27,16 +27,22 @@ module Kashiwamochi
|
|
27
27
|
html_options = options.delete(:html_options) || {}
|
28
28
|
default_order = options.delete(:default_order)
|
29
29
|
|
30
|
+
query = query.dup
|
30
31
|
attr_name = attribute.to_s
|
31
|
-
|
32
|
+
current_sort = query.sort_param
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
sort.dir.downcase
|
37
|
-
].compact.join(' ')
|
34
|
+
classes = []
|
35
|
+
classes << (html_options[:class] || "#{attr_name}_#{Kashiwamochi.config.sort_link_class}")
|
36
|
+
classes << Kashiwamochi.config.sort_link_class
|
38
37
|
|
39
|
-
|
38
|
+
if current_sort.valid? && current_sort.key.to_s == attr_name
|
39
|
+
classes << current_sort.dir.downcase
|
40
|
+
query.sort_param = current_sort.toggle
|
41
|
+
else
|
42
|
+
query.sort_param = Kashiwamochi::Sort.new(attr_name, default_order)
|
43
|
+
end
|
44
|
+
|
45
|
+
html_options[:class] = classes.compact.join(' ')
|
40
46
|
options[Kashiwamochi.config.search_key] = query.to_option
|
41
47
|
|
42
48
|
name = args.shift || attr_name
|
data/lib/kashiwamochi/query.rb
CHANGED
@@ -1,21 +1,19 @@
|
|
1
1
|
require 'active_support/ordered_hash'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
3
|
|
3
4
|
module Kashiwamochi
|
4
5
|
class Query
|
5
|
-
attr_accessor :search_params, :
|
6
|
+
attr_accessor :search_params, :sort_param
|
6
7
|
|
7
8
|
def initialize(attributes = {})
|
8
|
-
|
9
|
-
|
9
|
+
attributes = attributes.dup
|
10
|
+
attributes.with_indifferent_access
|
10
11
|
|
11
|
-
|
12
|
+
@search_params = ActiveSupport::OrderedHash.new.with_indifferent_access
|
13
|
+
@sort_param = Sort.parse(attributes.delete(Kashiwamochi.config.sort_key))
|
12
14
|
|
13
15
|
attributes.each do |key, value|
|
14
|
-
|
15
|
-
add_sort_param(key, value)
|
16
|
-
else
|
17
|
-
add_search_param(key, value)
|
18
|
-
end
|
16
|
+
add_search_param(key, value)
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
@@ -23,7 +21,7 @@ module Kashiwamochi
|
|
23
21
|
unless @search_params.key? key
|
24
22
|
instance_eval <<-METHOD
|
25
23
|
def attribute_#{key}
|
26
|
-
search = @search_params[
|
24
|
+
search = @search_params[:#{key}]
|
27
25
|
search.value
|
28
26
|
end
|
29
27
|
alias original_#{key} #{key} if defined? #{key}
|
@@ -35,14 +33,6 @@ module Kashiwamochi
|
|
35
33
|
@search_params[search.key] = search
|
36
34
|
end
|
37
35
|
|
38
|
-
def add_sort_param(key, value)
|
39
|
-
values = value.is_a?(Array) ? value : [value]
|
40
|
-
values.each do |v|
|
41
|
-
sort = Sort.parse(v)
|
42
|
-
@sort_params[sort.key] = sort if sort.valid?
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
36
|
def respond_to?(method_id, include_private = false)
|
47
37
|
super || respond_to_missing?(method_id, include_private)
|
48
38
|
end
|
@@ -62,23 +52,24 @@ module Kashiwamochi
|
|
62
52
|
end
|
63
53
|
end
|
64
54
|
|
65
|
-
def
|
55
|
+
def sort_query(*keys)
|
66
56
|
allowed_keys = keys.flatten.map(&:to_s).uniq
|
67
|
-
|
68
|
-
|
57
|
+
if allowed_keys.empty? || allowed_keys.include?(@sort_param.key.to_s)
|
58
|
+
@sort_param.to_query
|
59
|
+
else
|
60
|
+
nil
|
69
61
|
end
|
70
|
-
allowed_sorts.empty? ? nil : allowed_sorts.map(&:to_query).join(', ')
|
71
62
|
end
|
72
|
-
alias_method :
|
63
|
+
alias_method :sort, :sort_query
|
73
64
|
|
74
65
|
def to_option
|
75
66
|
hash = Hash[*@search_params.values.map { |search| [search.key, search.value] }.flatten]
|
76
|
-
hash[Kashiwamochi.config.sort_key] = @
|
67
|
+
hash[Kashiwamochi.config.sort_key] = @sort_param.to_query
|
77
68
|
hash
|
78
69
|
end
|
79
70
|
|
80
71
|
def inspect
|
81
|
-
"<Query search: #{@search_params}, sort: #{@
|
72
|
+
"<Query search: #{@search_params}, sort: #{@sort_param}>"
|
82
73
|
end
|
83
74
|
|
84
75
|
def persisted?
|
data/lib/kashiwamochi/sort.rb
CHANGED
@@ -41,7 +41,7 @@ module Kashiwamochi
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def valid?
|
44
|
-
|
44
|
+
!@key.empty?
|
45
45
|
end
|
46
46
|
|
47
47
|
def to_query
|
@@ -53,6 +53,7 @@ module Kashiwamochi
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def self.sanitize(value)
|
56
|
+
value = value.first if value.is_a?(Array)
|
56
57
|
value.to_s.strip
|
57
58
|
end
|
58
59
|
|
data/lib/kashiwamochi/version.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Kashiwamochi::ActionView do
|
4
4
|
describe '#search_form_for' do
|
5
5
|
before do
|
6
|
-
@q = Kashiwamochi::Query.new(:name => 'test', :s =>
|
6
|
+
@q = Kashiwamochi::Query.new(:name => 'test', :s => 'name desc')
|
7
7
|
helper.search_form_for @q, :url => {:controller => 'users', :action => 'index'} do |f|
|
8
8
|
@f = f
|
9
9
|
end
|
@@ -14,14 +14,64 @@ describe Kashiwamochi::ActionView do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
describe '#search_sort_link_to' do
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
context 'with sort' do
|
18
|
+
before do
|
19
|
+
@q = Kashiwamochi::Query.new(:name => 'test', :s => 'name desc')
|
20
|
+
@link1 = helper.search_sort_link_to(@q, :name, 'User name', :controller => 'users', :action => 'index')
|
21
|
+
@link2 = helper.search_sort_link_to(@q, :age, 'User Age', :controller => 'users', :action => 'index')
|
22
|
+
@link3 = helper.search_sort_link_to(@q, :sex, 'User Sex', :controller => 'users', :action => 'index', :default_order => 'desc')
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'name' do
|
26
|
+
subject { @link1 }
|
27
|
+
it { should match %r(href="/users\?q%5Bname%5D=test&q%5Bs%5D=name\+asc") }
|
28
|
+
it { should match %r(class="name_sort_link sort_link desc") }
|
29
|
+
it { should match %r(User name) }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'age' do
|
33
|
+
subject { @link2 }
|
34
|
+
it { should match %r(href="/users\?q%5Bname%5D=test&q%5Bs%5D=age\+asc") }
|
35
|
+
it { should match %r(class="age_sort_link sort_link") }
|
36
|
+
it { should match %r(User Age) }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'sex' do
|
40
|
+
subject { @link3 }
|
41
|
+
it { should match %r(href="/users\?q%5Bname%5D=test&q%5Bs%5D=sex\+desc") }
|
42
|
+
it { should match %r(class="sex_sort_link sort_link") }
|
43
|
+
it { should match %r(User Sex) }
|
44
|
+
end
|
20
45
|
end
|
21
|
-
subject { @link }
|
22
46
|
|
23
|
-
|
24
|
-
|
25
|
-
|
47
|
+
context 'without sort' do
|
48
|
+
before do
|
49
|
+
@q = Kashiwamochi::Query.new(:name => 'test')
|
50
|
+
@link1 = helper.search_sort_link_to(@q, :name, 'User name', :controller => 'users', :action => 'index')
|
51
|
+
@link2 = helper.search_sort_link_to(@q, :age, 'User age', :controller => 'users', :action => 'index')
|
52
|
+
@link3 = helper.search_sort_link_to(@q, :sex, 'User sex', :controller => 'users', :action => 'index', :default_order => 'desc')
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'name' do
|
56
|
+
subject { @link1 }
|
57
|
+
it { should match %r(href="/users\?q%5Bname%5D=test&q%5Bs%5D=name\+asc") }
|
58
|
+
it { should match %r(class="name_sort_link sort_link") }
|
59
|
+
it { should match %r(User name) }
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'age' do
|
63
|
+
subject { @link2 }
|
64
|
+
it { should match %r(href="/users\?q%5Bname%5D=test&q%5Bs%5D=age\+asc") }
|
65
|
+
it { should match %r(class="age_sort_link sort_link") }
|
66
|
+
it { should match %r(User age) }
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'sex' do
|
70
|
+
subject { @link3 }
|
71
|
+
it { should match %r(href="/users\?q%5Bname%5D=test&q%5Bs%5D=sex\+desc") }
|
72
|
+
it { should match %r(class="sex_sort_link sort_link") }
|
73
|
+
it { should match %r(User sex) }
|
74
|
+
end
|
75
|
+
end
|
26
76
|
end
|
27
77
|
end
|
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Kashiwamochi::Query do
|
4
4
|
describe '#initialize' do
|
5
|
-
context 'with {:foo => 1, :bar => 2, :to_s => 3, :s =>
|
6
|
-
before { @q = Kashiwamochi::Query.new(:foo => 1, :bar => 2, :to_s => 3, :s =>
|
5
|
+
context 'with {:foo => 1, :bar => 2, :to_s => 3, :s => "name asc"]}' do
|
6
|
+
before { @q = Kashiwamochi::Query.new(:foo => 1, :bar => 2, :to_s => 3, :s => "name asc") }
|
7
7
|
subject { @q }
|
8
8
|
|
9
9
|
describe 'length' do
|
@@ -11,24 +11,19 @@ describe Kashiwamochi::Query do
|
|
11
11
|
subject { @q.search_params }
|
12
12
|
its(:length) { should eq 3 }
|
13
13
|
end
|
14
|
-
|
15
|
-
context 'sort_params' do
|
16
|
-
subject { @q.sort_params }
|
17
|
-
its(:length) { should eq 2 }
|
18
|
-
end
|
19
14
|
end
|
20
15
|
|
21
|
-
|
16
|
+
describe 'having' do
|
22
17
|
its(:foo) { should eq 1 }
|
23
18
|
its(:bar) { should eq 2 }
|
24
19
|
its(:to_s) { should eq 3 }
|
25
20
|
end
|
26
21
|
|
27
|
-
|
22
|
+
describe 'missing' do
|
28
23
|
its(:buzz) { should be_nil }
|
29
24
|
end
|
30
25
|
|
31
|
-
|
26
|
+
describe 'alias' do
|
32
27
|
its(:attribute_foo) { should eq 1 }
|
33
28
|
its(:original_to_s) { should be_an_instance_of String }
|
34
29
|
end
|
@@ -36,14 +31,14 @@ describe Kashiwamochi::Query do
|
|
36
31
|
end
|
37
32
|
end
|
38
33
|
|
39
|
-
describe '#
|
40
|
-
context 'build with {:s =>
|
41
|
-
before { @q = Kashiwamochi::Query.new(:s =>
|
42
|
-
subject { @q.
|
34
|
+
describe '#sort_query' do
|
35
|
+
context 'build with {:s => "name asc"}' do
|
36
|
+
before { @q = Kashiwamochi::Query.new(:s => "name asc") }
|
37
|
+
subject { @q.sort_query(keys) }
|
43
38
|
|
44
39
|
context 'with empty' do
|
45
40
|
let(:keys) { [] }
|
46
|
-
it { should eq 'name asc
|
41
|
+
it { should eq 'name asc' }
|
47
42
|
end
|
48
43
|
|
49
44
|
context 'with :name' do
|
@@ -53,12 +48,12 @@ describe Kashiwamochi::Query do
|
|
53
48
|
|
54
49
|
context 'with :created_at' do
|
55
50
|
let(:keys) { [:created_at] }
|
56
|
-
it { should
|
51
|
+
it { should be_nil }
|
57
52
|
end
|
58
53
|
|
59
54
|
context 'with [:name, :created_at]' do
|
60
55
|
let(:keys) { [:name, :created_at] }
|
61
|
-
it { should eq 'name asc
|
56
|
+
it { should eq 'name asc' }
|
62
57
|
end
|
63
58
|
|
64
59
|
context 'with [:foo, :bar]' do
|
@@ -69,10 +64,10 @@ describe Kashiwamochi::Query do
|
|
69
64
|
end
|
70
65
|
|
71
66
|
describe '#to_option' do
|
72
|
-
before { @q = Kashiwamochi::Query.new(:name => 'aira', :s =>
|
67
|
+
before { @q = Kashiwamochi::Query.new(:name => 'aira', :s => "created_at desc") }
|
73
68
|
subject { @q.to_option }
|
74
69
|
it { should be_an_instance_of Hash }
|
75
|
-
it { should eq ({:name => 'aira', :s =>
|
70
|
+
it { should eq ({:name => 'aira', :s => 'created_at desc'}) }
|
76
71
|
end
|
77
72
|
|
78
73
|
describe '#build' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kashiwamochi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
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-
|
12
|
+
date: 2012-05-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: railties
|
@@ -152,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
152
|
version: '0'
|
153
153
|
segments:
|
154
154
|
- 0
|
155
|
-
hash:
|
155
|
+
hash: 373352056748660302
|
156
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
157
|
none: false
|
158
158
|
requirements:
|
@@ -161,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
161
|
version: '0'
|
162
162
|
segments:
|
163
163
|
- 0
|
164
|
-
hash:
|
164
|
+
hash: 373352056748660302
|
165
165
|
requirements: []
|
166
166
|
rubyforge_project: kashiwamochi
|
167
167
|
rubygems_version: 1.8.21
|