tablets 0.3.6 → 0.3.7
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.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/tablets/data/processing/filter.rb +8 -8
- data/lib/tablets/utils/search_condition.rb +56 -0
- data/lib/tablets/version.rb +1 -1
- metadata +3 -3
- data/lib/tablets/utils/arel.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fabd1f1477cd295cede4c19f5401f3200a19dd2d
|
4
|
+
data.tar.gz: 7dbfdf2ae6f5e4ee8c7fc40c351ef4b1130e3d50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96286ea703337dd2e0123835c8fc9a41c35455a1c3b1680e178ec0bcab57d1292fd91cce2e0026e15c96c07ad6e962947bdfb461568741cd38107e7d6d9f1da3
|
7
|
+
data.tar.gz: 3ea819cbd7b92d3d0ffb38f9ac3bc77333877e7515c9af05df0d9bcb5a56d06525f97461eb3df1e472613ca682ac3c7d0e3e50120be3c6c8739efdee2c4386a3
|
data/README.md
CHANGED
@@ -58,7 +58,13 @@ Tablets.register :posts do
|
|
58
58
|
options: { className: 'post-title' }
|
59
59
|
}, {
|
60
60
|
title: 'User',
|
61
|
-
data: proc
|
61
|
+
data: proc do |post, controller|
|
62
|
+
if post.user == controller.current_user
|
63
|
+
'It is you'
|
64
|
+
else
|
65
|
+
[post.user.first_name, post.user.last_name].compact.join(' ')
|
66
|
+
end
|
67
|
+
end,
|
62
68
|
order: ['users.first_name', 'users.last_name'],
|
63
69
|
search: ['users.first_name', 'users.last_name']
|
64
70
|
}]
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'active_support/core_ext/object'
|
2
2
|
|
3
|
-
require 'tablets/utils/
|
3
|
+
require 'tablets/utils/search_condition'
|
4
4
|
|
5
5
|
require 'tablets/data/processing/base'
|
6
6
|
|
@@ -28,10 +28,8 @@ module Tablets
|
|
28
28
|
# Builds search conditions.
|
29
29
|
def build_conditions_for(query)
|
30
30
|
query.split(' ').map do |value|
|
31
|
-
|
32
|
-
|
33
|
-
end.reduce(:or)
|
34
|
-
end.reduce(:and)
|
31
|
+
'(' + search_conditions(value) + ')'
|
32
|
+
end.join(' AND ')
|
35
33
|
end
|
36
34
|
|
37
35
|
# Returns searchable columns.
|
@@ -39,9 +37,11 @@ module Tablets
|
|
39
37
|
columns.map { |column| column[:search] }.flatten.compact
|
40
38
|
end
|
41
39
|
|
42
|
-
# Returs search
|
43
|
-
def
|
44
|
-
|
40
|
+
# Returs search conditions for each searchable column.
|
41
|
+
def search_conditions(value)
|
42
|
+
searchable_columns.map do |column|
|
43
|
+
Tablets::Utils::SearchCondition.build(column, value)
|
44
|
+
end.join(' OR ')
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Tablets
|
2
|
+
module Utils
|
3
|
+
# Resposible for building database specific search conditions.
|
4
|
+
module SearchCondition
|
5
|
+
class << self
|
6
|
+
# Builds search condition for specific db type.
|
7
|
+
def build(column, query)
|
8
|
+
sanitize "(#{casted_column(column)} #{like} ?)", "%#{query}%"
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
# Sanitizes sql expression with params.
|
14
|
+
def sanitize(sql, *params)
|
15
|
+
ActiveRecord::Base.send(:sanitize_sql_array, [sql, *params])
|
16
|
+
end
|
17
|
+
|
18
|
+
# Casyt column to string.
|
19
|
+
def casted_column(column)
|
20
|
+
"CAST (#{quoted_column(column)} AS #{string_type})"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Quotes column name.
|
24
|
+
def quoted_column(column)
|
25
|
+
column.split('.').map do |component|
|
26
|
+
ActiveRecord::Base.connection.quote_column_name component
|
27
|
+
end.join('.')
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns database specific like operator.
|
31
|
+
def like
|
32
|
+
case db_adapter
|
33
|
+
when :postgresql then 'ILIKE'
|
34
|
+
when :mysql2 then 'LIKE'
|
35
|
+
when :sqlite3 then 'LIKE'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns database specific string type.
|
40
|
+
def string_type
|
41
|
+
case db_adapter
|
42
|
+
when :postgresql then 'VARCHAR'
|
43
|
+
when :mysql2 then 'CHAR'
|
44
|
+
when :sqlite3 then 'TEXT'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Retrieves rails database adapter.
|
49
|
+
def db_adapter
|
50
|
+
@db_adapter ||=
|
51
|
+
ActiveRecord::Base.configurations[Rails.env]['adapter'].to_sym
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/tablets/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tablets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yevhen Shemet
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -126,8 +126,8 @@ files:
|
|
126
126
|
- lib/tablets/railtie.rb
|
127
127
|
- lib/tablets/renderer.rb
|
128
128
|
- lib/tablets/tablet.rb
|
129
|
-
- lib/tablets/utils/arel.rb
|
130
129
|
- lib/tablets/utils/config.rb
|
130
|
+
- lib/tablets/utils/search_condition.rb
|
131
131
|
- lib/tablets/version.rb
|
132
132
|
- lib/tablets/view_helpers.rb
|
133
133
|
- spec/lib/tablets/data/processing/base_spec.rb
|
data/lib/tablets/utils/arel.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
module Tablets
|
2
|
-
module Utils
|
3
|
-
# Arel related utils.
|
4
|
-
module Arel
|
5
|
-
class << self
|
6
|
-
# Casting arel column using db specific type.
|
7
|
-
def column(column)
|
8
|
-
model, column = column.split('.')
|
9
|
-
model = model.singularize.titleize.gsub(/ /, '').constantize
|
10
|
-
|
11
|
-
::Arel::Nodes::NamedFunction.new(
|
12
|
-
'CAST', [model.arel_table[column.to_sym].as(typecast)]
|
13
|
-
)
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
# Returns database specific string type.
|
19
|
-
def typecast
|
20
|
-
case db_adapter
|
21
|
-
when :postgresql then 'VARCHAR'
|
22
|
-
when :mysql2 then 'CHAR'
|
23
|
-
when :sqlite3 then 'TEXT'
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# Retrieves rails database adapter.
|
28
|
-
def db_adapter
|
29
|
-
ActiveRecord::Base.configurations[Rails.env]['adapter'].to_sym
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|