livelist-rails 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/lib/livelist/rails/active_record.rb +119 -102
- data/lib/livelist/rails/version.rb +1 -1
- metadata +8 -7
data/CHANGELOG.md
ADDED
@@ -6,135 +6,152 @@ module Livelist
|
|
6
6
|
|
7
7
|
def filters(*filter_slugs)
|
8
8
|
@@filter_slugs = filter_slugs
|
9
|
+
@@counts = {}
|
10
|
+
@@option_objects = {}
|
11
|
+
|
9
12
|
@@filter_slugs.each do |filter_slug|
|
10
|
-
filter_slug
|
11
|
-
|
13
|
+
define_class_methods(filter_slug)
|
14
|
+
end
|
12
15
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
def filters_as_json(filter_params)
|
17
|
+
@@filter_params = filter_params || {}
|
18
|
+
@@filter_slugs.map do |filter|
|
19
|
+
filter_options = send("#{filter}_filters", filter)
|
20
|
+
send("#{filter}_filter", filter_options)
|
21
|
+
end
|
22
|
+
end
|
17
23
|
|
18
|
-
|
19
|
-
|
20
|
-
|
24
|
+
def filter_relation(filter_params)
|
25
|
+
relation = scoped
|
26
|
+
filter_params.each do |filter, values|
|
27
|
+
relation = relation.send("#{filter}_relation", values)
|
28
|
+
end
|
29
|
+
relation
|
30
|
+
end
|
21
31
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
:options => options
|
27
|
-
}
|
28
|
-
end
|
32
|
+
def filter(filter_params)
|
33
|
+
filter_params ||= {}
|
34
|
+
@@filter_relation = filter_relation(filter_params)
|
35
|
+
end
|
29
36
|
|
30
|
-
|
31
|
-
|
32
|
-
|
37
|
+
def filter_option_count(filter_slug, option)
|
38
|
+
@@counts[filter_slug] = send("#{filter_slug}_filter_counts") unless @@counts.has_key?(filter_slug)
|
39
|
+
@@counts[filter_slug][option.to_s] || 0
|
40
|
+
end
|
33
41
|
|
34
|
-
|
35
|
-
|
36
|
-
|
42
|
+
def cached_option_objects(filter_slug)
|
43
|
+
@@option_objects[filter_slug] = send("#{filter_slug}_filter_option_objects") unless @@option_objects.has_key?(filter_slug)
|
44
|
+
@@option_objects[filter_slug]
|
45
|
+
end
|
46
|
+
end
|
37
47
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
if option_objects.any?{|object| object.kind_of?(Hash) && object.has_key?(key)}
|
42
|
-
option_objects.map{|object| object[key]}
|
43
|
-
elsif option_objects.any?{|object| object.respond_to?(key)}
|
44
|
-
option_objects.map(&key)
|
45
|
-
end
|
46
|
-
end
|
48
|
+
def define_class_methods(filter_slug)
|
49
|
+
filter_slug = filter_slug.to_s
|
50
|
+
metaclass = class << self; self; end
|
47
51
|
|
48
|
-
|
49
|
-
|
50
|
-
|
52
|
+
metaclass.instance_eval do
|
53
|
+
define_method "#{filter_slug}_filter_name" do
|
54
|
+
filter_slug.capitalize
|
55
|
+
end
|
51
56
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
else
|
56
|
-
option.send(:id).to_s
|
57
|
-
end
|
58
|
-
end
|
57
|
+
define_method "#{filter_slug}_filter_slug" do
|
58
|
+
filter_slug
|
59
|
+
end
|
59
60
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
option_object.send(:name)
|
68
|
-
else
|
69
|
-
option.to_s
|
70
|
-
end
|
71
|
-
end
|
61
|
+
define_method "#{filter_slug}_filter" do |options|
|
62
|
+
{
|
63
|
+
:filter_slug => send("#{filter_slug}_filter_slug"),
|
64
|
+
:name => send("#{filter_slug}_filter_name"),
|
65
|
+
:options => options
|
66
|
+
}
|
67
|
+
end
|
72
68
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
else
|
77
|
-
option.send(:id).to_s
|
78
|
-
end
|
79
|
-
end
|
69
|
+
define_method "#{filter_slug}_filter_option_key_name" do
|
70
|
+
new.respond_to?(filter_slug.to_sym) ? filter_slug.to_sym : :id
|
71
|
+
end
|
80
72
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
end
|
85
|
-
class_variable_get(:"@@#{filter_slug}_filter_counts")[option.to_s] || 0
|
86
|
-
end
|
73
|
+
define_method "#{filter_slug}_filter_option_objects" do
|
74
|
+
select("distinct #{filter_slug}").all
|
75
|
+
end
|
87
76
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
:slug => option_slug,
|
96
|
-
:name => option_name,
|
97
|
-
:value => option_value,
|
98
|
-
:count => option_count,
|
99
|
-
:selected => selected
|
100
|
-
}
|
77
|
+
define_method "#{filter_slug}_filter_values" do
|
78
|
+
key = send("#{filter_slug}_filter_option_key_name")
|
79
|
+
option_objects = cached_option_objects(filter_slug)
|
80
|
+
if option_objects.any?{|object| object.kind_of?(Hash) && object.has_key?(key)}
|
81
|
+
option_objects.map{|object| object[key]}
|
82
|
+
elsif option_objects.any?{|object| object.respond_to?(key)}
|
83
|
+
option_objects.map(&key)
|
101
84
|
end
|
85
|
+
end
|
102
86
|
|
103
|
-
|
104
|
-
|
87
|
+
define_method "#{filter_slug}_filter_counts" do
|
88
|
+
::Rails.logger.error "*"*100
|
89
|
+
::Rails.logger.error "*"*100
|
90
|
+
::Rails.logger.error "*"*100
|
91
|
+
::Rails.logger.error group(filter_slug).count.inspect
|
92
|
+
::Rails.logger.error @@filter_relation.group(filter_slug).count.inspect
|
93
|
+
@@filter_relation.group(filter_slug).count
|
94
|
+
end
|
95
|
+
|
96
|
+
define_method "#{filter_slug}_filter_option_slug" do |option|
|
97
|
+
if [String, Integer].any?{|klass| option.kind_of?(klass)}
|
98
|
+
option.to_s
|
99
|
+
else
|
100
|
+
option.send(:id).to_s
|
105
101
|
end
|
102
|
+
end
|
106
103
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
104
|
+
define_method "#{filter_slug}_filter_option_name" do |option|
|
105
|
+
option_objects = cached_option_objects(filter_slug)
|
106
|
+
if option_objects.any?{|object| object.kind_of?(Hash) && object.has_key?(:name)}
|
107
|
+
option_object = option_objects.detect{|object| object[:state] == option.to_s}
|
108
|
+
option_object[:name]
|
109
|
+
elsif option_objects.any?{|object| object.respond_to?(:name)}
|
110
|
+
option_object = option_objects.detect{|object| object.send(:id).to_s == option.to_s}
|
111
|
+
option_object.send(:name)
|
112
|
+
else
|
113
|
+
option.to_s
|
112
114
|
end
|
115
|
+
end
|
113
116
|
|
114
|
-
|
115
|
-
|
117
|
+
define_method "#{filter_slug}_filter_option_value" do |option|
|
118
|
+
if [String, Integer].any?{|klass| option.kind_of?(klass)}
|
119
|
+
option.to_s
|
120
|
+
else
|
121
|
+
option.send(:id).to_s
|
116
122
|
end
|
117
123
|
end
|
118
|
-
end
|
119
124
|
|
120
|
-
|
121
|
-
|
122
|
-
@@filter_slugs.map do |filter|
|
123
|
-
filter_options = send("#{filter}_filters", filter_params[filter])
|
124
|
-
send("#{filter}_filter", filter_options)
|
125
|
+
define_method "#{filter_slug}_filter_option_count" do |option|
|
126
|
+
filter_option_count(filter_slug, option)
|
125
127
|
end
|
126
|
-
end
|
127
128
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
define_method "#{filter_slug}_filter_option" do |option, selected|
|
130
|
+
{
|
131
|
+
:slug => send("#{filter_slug}_filter_option_slug", option),
|
132
|
+
:name => send("#{filter_slug}_filter_option_name", option),
|
133
|
+
:value => send("#{filter_slug}_filter_option_value", option),
|
134
|
+
:count => send("#{filter_slug}_filter_option_count", option),
|
135
|
+
:selected => selected
|
136
|
+
}
|
137
|
+
end
|
138
|
+
|
139
|
+
define_method "#{filter_slug}_filter_option_selected?" do |filter, option|
|
140
|
+
@@filter_params[filter].nil? ? false : @@filter_params[filter].include?(option.to_s)
|
141
|
+
end
|
142
|
+
|
143
|
+
define_method "#{filter_slug}_filters" do |filter|
|
144
|
+
send("#{filter_slug}_filter_values").map do |option|
|
145
|
+
selected = send("#{filter_slug}_filter_option_selected?", filter, option)
|
146
|
+
send("#{filter_slug}_filter_option", option, selected)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
define_method "#{filter_slug}_relation" do |values|
|
151
|
+
where(model_name.to_s.tableize => { filter_slug => values })
|
133
152
|
end
|
134
|
-
query
|
135
153
|
end
|
136
154
|
end
|
137
|
-
|
138
155
|
end
|
139
156
|
end
|
140
157
|
end
|
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.
|
4
|
+
version: 0.0.6
|
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-01-
|
12
|
+
date: 2012-01-26 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &14627060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *14627060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
requirement: &
|
27
|
+
requirement: &14626640 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *14626640
|
36
36
|
description: ! 'livelist-rails is a Rails 3.1 Engine/Extensiont incorporating the
|
37
37
|
following javascript libraries: Mustache.js, underscore.js, jQuery and livelist.js,
|
38
38
|
and providing ActiveRecord filtering extenstions.'
|
@@ -44,6 +44,7 @@ extra_rdoc_files: []
|
|
44
44
|
files:
|
45
45
|
- .gitignore
|
46
46
|
- .rspec
|
47
|
+
- CHANGELOG.md
|
47
48
|
- Gemfile
|
48
49
|
- README.md
|
49
50
|
- Rakefile
|
@@ -83,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
84
|
version: '0'
|
84
85
|
requirements: []
|
85
86
|
rubyforge_project: livelist-rails
|
86
|
-
rubygems_version: 1.8.
|
87
|
+
rubygems_version: 1.8.10
|
87
88
|
signing_key:
|
88
89
|
specification_version: 3
|
89
90
|
summary: A Rails Engine/Extension Incorporating Livelist.js
|