auto_define_scope 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/auto_define_scope/active_record_extension.rb +16 -4
- data/lib/auto_define_scope/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b411e8fbe13d8df01d64c0eed8ac40eab0e88d8bd079a6fb95478a169ee260a
|
4
|
+
data.tar.gz: 9c24148854615f854cd18f5f3fb6f485e2b729341ec25e7bd7a4c08768cee1e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18d5a5df81ad4188fb132a47abd51e5940632742676b46e071d890a259231430e7966e5fbe2a2968c9a9a8f36da2be5c6d1dba06886a6094cbe47dd4dfc02f25
|
7
|
+
data.tar.gz: 2de98abcb378d8245acf94c1495897402454c4f36e56de7d0e2d74c7d758c739139bfbf7f90d2971c1765f220a6766b3a6a011f911e8dca626bbfbc46440374b
|
@@ -5,11 +5,11 @@ module AutoDefineScope
|
|
5
5
|
|
6
6
|
# For example, CustomerAgentHistory include this
|
7
7
|
# columns is a array like ["status", "p_way", {customer_agent: ["code", "name", agent: :province_id]}]
|
8
|
-
def search_columns
|
8
|
+
def search_columns(*columns)
|
9
9
|
generate_cope columns, :search
|
10
10
|
end
|
11
11
|
|
12
|
-
def with_columns
|
12
|
+
def with_columns(*columns)
|
13
13
|
generate_cope columns, :with
|
14
14
|
end
|
15
15
|
|
@@ -26,7 +26,7 @@ module AutoDefineScope
|
|
26
26
|
end
|
27
27
|
|
28
28
|
protected
|
29
|
-
def generate_cope
|
29
|
+
def generate_cope(columns, type, klass = self, joins_params_array = [])
|
30
30
|
columns.each do |c|
|
31
31
|
if c.is_a?(Hash)
|
32
32
|
c.keys.each do |k|
|
@@ -45,17 +45,29 @@ module AutoDefineScope
|
|
45
45
|
end
|
46
46
|
next
|
47
47
|
end
|
48
|
+
column_type = columns_hash[c.to_s]&.type
|
49
|
+
raise "scopes defined error, not found column('#{c}') for table #{klass.table_name}" if column.nil?
|
48
50
|
joins_params = joins_params_array.reverse.inject() { |a, n| { n => a } }
|
49
51
|
table_name = klass.table_name
|
50
52
|
@auto_define_scopes ||= []
|
51
53
|
scope_name = "#{type}_#{joins_params_array.last&.to_s&.+ '_'}#{c}"
|
52
54
|
@auto_define_scopes << scope_name
|
53
55
|
scope scope_name, ->(v) do
|
54
|
-
value =
|
56
|
+
value = case column_type
|
57
|
+
when :integer
|
58
|
+
value.to_i
|
59
|
+
when :string
|
60
|
+
value.to_s
|
61
|
+
end
|
62
|
+
value = type == :with ? value : "%#{value}%"
|
55
63
|
joins(joins_params).where("#{table_name}.#{c} #{OPERATE_SQL[type]}", value)
|
56
64
|
end
|
57
65
|
end
|
58
66
|
nil
|
59
67
|
end
|
68
|
+
|
69
|
+
def convert_type(obj, type)
|
70
|
+
|
71
|
+
end
|
60
72
|
end
|
61
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_define_scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tinyfeng.hou
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|