kashiwamochi 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/kashiwamochi/action_view.rb +5 -6
- data/lib/kashiwamochi/query.rb +44 -14
- data/lib/kashiwamochi/version.rb +1 -1
- data/spec/kashiwamochi/query_spec.rb +52 -25
- metadata +4 -4
|
@@ -27,23 +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
|
|
31
30
|
attr_name = attribute.to_s
|
|
32
|
-
current_sort = query.
|
|
31
|
+
current_sort = query.sort_params[attr_name]
|
|
33
32
|
|
|
34
33
|
classes = []
|
|
35
34
|
classes << (html_options[:class] || "#{attr_name}_#{Kashiwamochi.config.sort_link_class}")
|
|
36
35
|
classes << Kashiwamochi.config.sort_link_class
|
|
37
36
|
|
|
38
|
-
if current_sort
|
|
37
|
+
if current_sort
|
|
39
38
|
classes << current_sort.dir.downcase
|
|
40
|
-
query.
|
|
39
|
+
query.sort_params[attr_name] = current_sort.toggle
|
|
41
40
|
else
|
|
42
|
-
query.
|
|
41
|
+
query.sort_params[attr_name] = Kashiwamochi::Sort.new(attr_name, default_order)
|
|
43
42
|
end
|
|
44
43
|
|
|
45
44
|
html_options[:class] = classes.compact.join(' ')
|
|
46
|
-
options[Kashiwamochi.config.search_key] = query.to_option
|
|
45
|
+
options[Kashiwamochi.config.search_key] = query.to_option(attr_name)
|
|
47
46
|
|
|
48
47
|
name = args.shift || attr_name
|
|
49
48
|
link_to(name, options, html_options)
|
data/lib/kashiwamochi/query.rb
CHANGED
|
@@ -3,17 +3,19 @@ require 'active_support/core_ext/hash/indifferent_access'
|
|
|
3
3
|
|
|
4
4
|
module Kashiwamochi
|
|
5
5
|
class Query
|
|
6
|
-
attr_accessor :search_params, :
|
|
6
|
+
attr_accessor :search_params, :sort_params
|
|
7
7
|
|
|
8
8
|
def initialize(attributes = {})
|
|
9
|
-
attributes = attributes.dup
|
|
10
|
-
attributes.with_indifferent_access
|
|
11
|
-
|
|
12
9
|
@search_params = ActiveSupport::OrderedHash.new.with_indifferent_access
|
|
13
|
-
@
|
|
10
|
+
@sort_params = ActiveSupport::OrderedHash.new.with_indifferent_access
|
|
11
|
+
sort_key = Kashiwamochi.config.sort_key.to_s
|
|
14
12
|
|
|
15
13
|
attributes.each do |key, value|
|
|
16
|
-
|
|
14
|
+
if key.to_s == sort_key
|
|
15
|
+
add_sort_param key, value
|
|
16
|
+
else
|
|
17
|
+
add_search_param key, value
|
|
18
|
+
end
|
|
17
19
|
end
|
|
18
20
|
end
|
|
19
21
|
|
|
@@ -33,6 +35,14 @@ module Kashiwamochi
|
|
|
33
35
|
@search_params[search.key] = search
|
|
34
36
|
end
|
|
35
37
|
|
|
38
|
+
def add_sort_param(key, value)
|
|
39
|
+
values = Array(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
|
+
|
|
36
46
|
def respond_to?(method_id, include_private = false)
|
|
37
47
|
super || respond_to_missing?(method_id, include_private)
|
|
38
48
|
end
|
|
@@ -52,24 +62,44 @@ module Kashiwamochi
|
|
|
52
62
|
end
|
|
53
63
|
end
|
|
54
64
|
|
|
55
|
-
def
|
|
65
|
+
def sorts_query(*keys)
|
|
66
|
+
options = keys.extract_options!
|
|
67
|
+
|
|
56
68
|
allowed_keys = keys.flatten.map(&:to_s).uniq
|
|
57
|
-
|
|
58
|
-
|
|
69
|
+
allowed_sorts = []
|
|
70
|
+
|
|
71
|
+
unless allowed_keys.empty?
|
|
72
|
+
if options[:unordered]
|
|
73
|
+
allowed_sorts = @sort_params.values
|
|
74
|
+
allowed_sorts.reject! { |s| !allowed_keys.include?(s.key.to_s) }
|
|
75
|
+
else
|
|
76
|
+
allowed_keys.each { |key| allowed_sorts << @sort_params[key] if @sort_params.key? key }
|
|
77
|
+
end
|
|
59
78
|
else
|
|
60
|
-
|
|
79
|
+
allowed_sorts = @sort_params.values
|
|
61
80
|
end
|
|
81
|
+
|
|
82
|
+
allowed_sorts.empty? ? nil : allowed_sorts.map(&:to_query).join(', ')
|
|
62
83
|
end
|
|
63
|
-
alias_method :
|
|
84
|
+
alias_method :sorts, :sorts_query
|
|
85
|
+
|
|
86
|
+
def to_option(*sort_keys)
|
|
87
|
+
sort_keys = sort_keys.flatten.map(&:to_s).uniq
|
|
88
|
+
sorts = @sort_params.values
|
|
89
|
+
sorts.reject! { |s| !sort_keys.include?(s.key.to_s) } unless sort_keys.empty?
|
|
64
90
|
|
|
65
|
-
def to_option
|
|
66
91
|
hash = Hash[*@search_params.values.map { |search| [search.key, search.value] }.flatten]
|
|
67
|
-
|
|
92
|
+
unless sorts.empty?
|
|
93
|
+
hash[Kashiwamochi.config.sort_key] = case sorts.length
|
|
94
|
+
when 1 then sorts.first.to_query
|
|
95
|
+
else sorts.map(&:to_query)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
68
98
|
hash
|
|
69
99
|
end
|
|
70
100
|
|
|
71
101
|
def inspect
|
|
72
|
-
"<Query search: #{@search_params}, sort: #{@
|
|
102
|
+
"<Query search: #{@search_params}, sort: #{@sort_params}>"
|
|
73
103
|
end
|
|
74
104
|
|
|
75
105
|
def persisted?
|
data/lib/kashiwamochi/version.rb
CHANGED
|
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe Kashiwamochi::Query do
|
|
4
4
|
describe '#initialize' do
|
|
5
|
-
context
|
|
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', ' ', 'created_at desc']}" do
|
|
6
|
+
before { @q = Kashiwamochi::Query.new(:foo => 1, :bar => 2, :to_s => 3, :s => ['name asc', ' ', 'created_at desc']) }
|
|
7
7
|
subject { @q }
|
|
8
8
|
|
|
9
9
|
describe 'length' do
|
|
@@ -11,6 +11,11 @@ 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
|
|
14
19
|
end
|
|
15
20
|
|
|
16
21
|
describe 'having' do
|
|
@@ -31,40 +36,62 @@ describe Kashiwamochi::Query do
|
|
|
31
36
|
end
|
|
32
37
|
end
|
|
33
38
|
|
|
34
|
-
describe '#
|
|
35
|
-
context
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
describe '#sorts_query' do
|
|
40
|
+
context "build with {:s => ['name asc', ' ', 'created_at desc']}" do
|
|
41
|
+
context 'default' do
|
|
42
|
+
before { @q = Kashiwamochi::Query.new(:s => ['name asc', ' ', 'created_at desc']) }
|
|
43
|
+
subject { @q.sorts_query(keys) }
|
|
38
44
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
context 'with empty' do
|
|
46
|
+
let(:keys) { [] }
|
|
47
|
+
it { should eq 'name asc, created_at desc' }
|
|
48
|
+
end
|
|
43
49
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
context 'with :name' do
|
|
51
|
+
let(:keys) { [:name] }
|
|
52
|
+
it { should eq 'name asc' }
|
|
53
|
+
end
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
context 'with :created_at' do
|
|
56
|
+
let(:keys) { [:created_at] }
|
|
57
|
+
it { should eq 'created_at desc' }
|
|
58
|
+
end
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
context 'with [:name, :created_at]' do
|
|
61
|
+
let(:keys) { [:name, :created_at] }
|
|
62
|
+
it { should eq 'name asc, created_at desc' }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
context 'with [:created_at, :name]' do
|
|
66
|
+
let(:keys) { [:created_at, :name] }
|
|
67
|
+
it { should eq 'created_at desc, name asc' }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
context 'with [:foo, :bar]' do
|
|
71
|
+
let(:keys) { [:foo, :bar] }
|
|
72
|
+
it { should be_nil }
|
|
73
|
+
end
|
|
57
74
|
end
|
|
58
75
|
|
|
59
|
-
context '
|
|
60
|
-
|
|
61
|
-
|
|
76
|
+
context 'unordered' do
|
|
77
|
+
before { @q = Kashiwamochi::Query.new(:s => ['name asc', ' ', 'created_at desc']) }
|
|
78
|
+
subject { @q.sorts_query(keys, :unordered => true) }
|
|
79
|
+
|
|
80
|
+
context 'with [:name, :created_at]' do
|
|
81
|
+
let(:keys) { [:name, :created_at] }
|
|
82
|
+
it { should eq 'name asc, created_at desc' }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
context 'with [:created_at, :name]' do
|
|
86
|
+
let(:keys) { [:created_at, :name] }
|
|
87
|
+
it { should eq 'name asc, created_at desc' }
|
|
88
|
+
end
|
|
62
89
|
end
|
|
63
90
|
end
|
|
64
91
|
end
|
|
65
92
|
|
|
66
93
|
describe '#to_option' do
|
|
67
|
-
before { @q = Kashiwamochi::Query.new(:name => 'aira', :s => "created_at desc") }
|
|
94
|
+
before { @q = Kashiwamochi::Query.new(:name => 'aira', :s => ["created_at desc"]) }
|
|
68
95
|
subject { @q.to_option }
|
|
69
96
|
it { should be_an_instance_of Hash }
|
|
70
97
|
it { should eq ({:name => 'aira', :s => 'created_at desc'}) }
|
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.4.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-05-
|
|
12
|
+
date: 2012-05-02 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: -3790293919526034477
|
|
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: -3790293919526034477
|
|
165
165
|
requirements: []
|
|
166
166
|
rubyforge_project: kashiwamochi
|
|
167
167
|
rubygems_version: 1.8.21
|