active_tools 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a759da5f5f11959c0e31a7b1f54bf95cb7fa5277
4
- data.tar.gz: 8fcef87b170bfca3ee167b2a6ea7e95d2f0d9c3d
3
+ metadata.gz: a0201fdba3d947f57909f521dcf3b02133bec417
4
+ data.tar.gz: 80157e9273b0359769f8008c3537d908b4a75885
5
5
  SHA512:
6
- metadata.gz: c1ae1550cd0e61ab5f86dae9d8844fd69a6a247a5781afcd3a81ad1b41b20642f1e79d4f1c44ec7c2c6b0223abc7db6b59fe4ac0dd7d6ca42f7af3b5e90fb261
7
- data.tar.gz: 4915201db5c96629f095872d8692a80ba93208d269fd0c9b049c9c87a0d924a3638437cebe445ba1683062dd40722f3a942efb68675b6f08d844190b4ca9a4b6
6
+ metadata.gz: 692f52269b9f52d10e061d31821f695b896789b34d3b2fdfdb3014c389f7bd5c6a7e86f6a5223efb90b9db56c2ba4ae1ebe5808699e8b8b59361cd439cc5e2d8
7
+ data.tar.gz: 3be146c05980f3daaedf5beac48763afe9429dd35d4cd0883268ccb8b61c085ab493852a960a9cc0dd18f0bff2b418f69277690c34a04891c71aab77587064c8
@@ -0,0 +1,47 @@
1
+ module ActiveTools
2
+ module ActiveRecord
3
+ module PolymorphicConditions
4
+ extend ::ActiveSupport::Concern
5
+
6
+ included do
7
+ end
8
+
9
+ module ClassMethods
10
+ end
11
+
12
+ def polymorphic_conditions(*args)
13
+ conditions = []
14
+ table = self.class.table_name
15
+ stack = args.extract_options!
16
+ sql_queries = stack.collect do |as_resource, hash|
17
+ resource_queries = hash.map do |name, find_options|
18
+ resource_class = name.to_s.classify.constantize
19
+ resource_table = resource_class.table_name
20
+ conditions << resource_class.name
21
+ if find_options[:conditions].present?
22
+ conditions += find_options[:conditions][1..-1]
23
+ end
24
+ joins_clause =
25
+ Array.wrap(find_options[:join]).collect do |association|
26
+ reflection = resource_class.reflections[association]
27
+ if reflection.macro == :belongs_to && reflection.options[:polymorphic] != true
28
+ "INNER JOIN #{reflection.klass.table_name} ON #{reflection.active_record.table_name}.#{reflection.foreign_key} = #{reflection.klass.table_name}.id"
29
+ elsif reflection.macro.in?([:has_many, :has_one]) && reflection.options[:as].nil?
30
+ "INNER JOIN #{reflection.klass.table_name} ON #{reflection.klass.table_name}.#{reflection.foreign_key} = #{reflection.active_record.table_name}.id"
31
+ end
32
+ end.compact.join(" ").strip
33
+ "(#{table}.#{as_resource}_type = ? AND EXISTS(#{["SELECT 1 FROM #{resource_table}#{joins_clause.left_indent(1) if joins_clause.present?} WHERE #{resource_table}.id = #{table}.#{as_resource}_id", find_options[:conditions].first].compact.join(" AND ")}))"
34
+ end
35
+ "CASE WHEN #{table}.#{as_resource}_type IS NOT NULL AND #{table}.#{as_resource}_id IS NOT NULL THEN #{resource_queries.join(" OR ")} ELSE TRUE END"
36
+ end
37
+ conditions.insert(0, "#{sql_queries.join(" OR ")}".send_if(:round_with, "(",")") {|q| q.present?})
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+ module OnLoadActiveRecord
44
+ include ActiveRecord::PolymorphicConditions
45
+ end
46
+
47
+ end
@@ -2,6 +2,7 @@ require 'active_tools/active_record/record_id'
2
2
  require 'active_tools/active_record/adaptive_belongs_to'
3
3
  require 'active_tools/active_record/custom_counter_cache'
4
4
  require 'active_tools/active_record/with_permalink'
5
+ require 'active_tools/active_record/polymorphic_conditions'
5
6
 
6
7
  module ActiveTools
7
8
  module ActiveRecord
@@ -0,0 +1,19 @@
1
+ module ActiveTools
2
+ module CoreExtension
3
+
4
+ module ConcatConditions
5
+ module ArrayExtension
6
+ def concat_as_condition_with(*args)
7
+ options = args.extract_options!
8
+ conditions = args.first||[]
9
+ operator = options[:operator]||"AND"
10
+ round = options[:round]||false
11
+ concat_array = [self[0],conditions[0]].compact
12
+ sql_clause = concat_array.present? ? concat_array.join(" #{operator} ") : nil
13
+ [(round && concat_array.size > 1) ? "(#{sql_clause})" : sql_clause, *((self[1..-1]||[])+(conditions[1..-1]||[]))]
14
+ end
15
+ end
16
+ ::Array.send(:include, ArrayExtension)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveTools
2
+ module CoreExtension
3
+
4
+ module SendIf
5
+ module ObjectExtension
6
+ def send_if(*args, &block)
7
+ if yield(self) == true
8
+ send(*args)
9
+ else
10
+ self
11
+ end
12
+ end
13
+ end
14
+ ::Object.send(:include, ObjectExtension)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ module ActiveTools
2
+ module CoreExtension
3
+
4
+ module StringIndent
5
+ module StringExtension
6
+ def nobr
7
+ self.gsub(/\r/," ").gsub(/\n/," ")
8
+ end
9
+
10
+ def both_indent(*args)
11
+ indent_size = args.delete_at(0)
12
+ raise(TypeError, "Fixnum expected, #{indent_size.class} passed") unless indent_size.is_a?(Fixnum)
13
+ center(size+indent_size*2, *args)
14
+ end
15
+
16
+ def left_indent(*args)
17
+ indent_size = args.delete_at(0)
18
+ raise(TypeError, "Fixnum expected, #{indent_size.class} passed") unless indent_size.is_a?(Fixnum)
19
+ rjust(size+indent_size, *args)
20
+ end
21
+
22
+ def right_indent(*args)
23
+ indent_size = args.delete_at(0)
24
+ raise(TypeError, "Fixnum expected, #{indent_size.class} passed") unless indent_size.is_a?(Fixnum)
25
+ ljust(size+indent_size, *args)
26
+ end
27
+ end
28
+ ::String.send(:include, StringExtension)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveTools
2
+ module CoreExtension
3
+
4
+ module StringRoundWith
5
+ module StringExtension
6
+ def round_with(first, last = nil)
7
+ last ||= first
8
+ insert(0, first.to_s||"").insert(-1, last.to_s||"")
9
+ end
10
+ end
11
+ ::String.send(:include, StringExtension)
12
+ end
13
+ end
14
+ end
@@ -1,9 +1,13 @@
1
+ require 'active_tools/core_extension/send_if'
1
2
  require 'active_tools/core_extension/deep_copy'
2
3
  require 'active_tools/core_extension/deep_merge'
3
4
  require 'active_tools/core_extension/hashup'
4
5
  require 'active_tools/core_extension/merge_hashup'
5
6
  require 'active_tools/core_extension/kabuki'
6
7
  require 'active_tools/core_extension/method_digger'
8
+ require 'active_tools/core_extension/concat_conditions'
9
+ require 'active_tools/core_extension/string_indent'
10
+ require 'active_tools/core_extension/string_round_with'
7
11
 
8
12
  module ActiveTools
9
13
  module CoreExtension
@@ -1,3 +1,3 @@
1
1
  module ActiveTools
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valery Kvon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-19 00:00:00.000000000 Z
11
+ date: 2014-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -74,6 +74,7 @@ files:
74
74
  - lib/active_tools/active_record/adaptive_belongs_to/adapter.rb
75
75
  - lib/active_tools/active_record/custom_counter_cache.rb
76
76
  - lib/active_tools/active_record/custom_counter_cache/instance_methods.rb
77
+ - lib/active_tools/active_record/polymorphic_conditions.rb
77
78
  - lib/active_tools/active_record/record_id.rb
78
79
  - lib/active_tools/active_record/with_permalink.rb
79
80
  - lib/active_tools/activemodel.rb
@@ -81,6 +82,7 @@ files:
81
82
  - lib/active_tools/activesupport.rb
82
83
  - lib/active_tools/bundle.rb
83
84
  - lib/active_tools/core_extension.rb
85
+ - lib/active_tools/core_extension/concat_conditions.rb
84
86
  - lib/active_tools/core_extension/deep_copy.rb
85
87
  - lib/active_tools/core_extension/deep_merge.rb
86
88
  - lib/active_tools/core_extension/hashup.rb
@@ -90,6 +92,9 @@ files:
90
92
  - lib/active_tools/core_extension/kabuki/zip.rb
91
93
  - lib/active_tools/core_extension/merge_hashup.rb
92
94
  - lib/active_tools/core_extension/method_digger.rb
95
+ - lib/active_tools/core_extension/send_if.rb
96
+ - lib/active_tools/core_extension/string_indent.rb
97
+ - lib/active_tools/core_extension/string_round_with.rb
93
98
  - lib/active_tools/engine.rb
94
99
  - lib/active_tools/misc.rb
95
100
  - lib/active_tools/misc/input_source.rb