Module: ClosureTree::SupportAttributes
Instance Method Summary collapse
- #advisory_lock_name(instance = nil) ⇒ Object
- #advisory_lock_options ⇒ Object
- #dont_order_roots ⇒ Object
- #hierarchy_class_name ⇒ Object
- #name_column ⇒ Object
- #name_sym ⇒ Object
- #nulls_last_order_by ⇒ Object
- #order_by ⇒ Object
- #order_by_order(reverse = false) ⇒ Object
- #order_column ⇒ Object
- #order_column_sym ⇒ Object
- #parent_column_name ⇒ Object
- #parent_column_sym ⇒ Object
- #primary_key_column ⇒ Object
- #primary_key_type ⇒ Object
- #quoted_hierarchy_table_name ⇒ Object
- #quoted_id_column_name ⇒ Object
- #quoted_name_column ⇒ Object
- #quoted_order_column(include_table_name = true) ⇒ Object
- #quoted_parent_column_name ⇒ Object
- #quoted_table_name ⇒ Object
- #quoted_value(value) ⇒ Object
- #require_order_column ⇒ Object
-
#short_hierarchy_class_name ⇒ String
Returns the constant name of the hierarchy_class.
-
#t_alias_keyword ⇒ Object
table_name alias keyword , like “AS”.
Instance Method Details
#advisory_lock_name(instance = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/closure_tree/support_attributes.rb', line 11 def advisory_lock_name(instance = nil) if [:advisory_lock_name] case [:advisory_lock_name] when Proc proc = [:advisory_lock_name] proc.arity == 1 ? proc.call(base_class) : proc.call(base_class, instance) when Symbol if model_class.respond_to?([:advisory_lock_name]) model_class.send([:advisory_lock_name]) else raise ArgumentError, "Model #{model_class} does not respond to #{[:advisory_lock_name]}" end else [:advisory_lock_name].to_s end else "ct_#{Zlib.crc32(base_class.name.to_s).to_s(16)}" end end |
#advisory_lock_options ⇒ Object
31 32 33 |
# File 'lib/closure_tree/support_attributes.rb', line 31 def { timeout_seconds: [:advisory_lock_timeout_seconds] }.compact end |
#dont_order_roots ⇒ Object
102 103 104 |
# File 'lib/closure_tree/support_attributes.rb', line 102 def dont_order_roots [:dont_order_roots] || false end |
#hierarchy_class_name ⇒ Object
43 44 45 |
# File 'lib/closure_tree/support_attributes.rb', line 43 def hierarchy_class_name [:hierarchy_class_name] || "#{model_class}Hierarchy" end |
#name_column ⇒ Object
63 64 65 |
# File 'lib/closure_tree/support_attributes.rb', line 63 def name_column [:name_column] end |
#name_sym ⇒ Object
67 68 69 |
# File 'lib/closure_tree/support_attributes.rb', line 67 def name_sym name_column.to_sym end |
#nulls_last_order_by ⇒ Object
106 107 108 |
# File 'lib/closure_tree/support_attributes.rb', line 106 def nulls_last_order_by Arel.sql "-#{quoted_order_column} #{order_by_order(true)}" end |
#order_by ⇒ Object
98 99 100 |
# File 'lib/closure_tree/support_attributes.rb', line 98 def order_by [:order] end |
#order_by_order(reverse = false) ⇒ Object
110 111 112 113 114 |
# File 'lib/closure_tree/support_attributes.rb', line 110 def order_by_order(reverse = false) desc = !(order_by.to_s =~ /DESC\z/).nil? desc = !desc if reverse desc ? 'DESC' : 'ASC' end |
#order_column ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/closure_tree/support_attributes.rb', line 116 def order_column o = order_by if o.nil? nil elsif o.is_a?(String) o.split(' ', 2).first else o.to_s end end |
#order_column_sym ⇒ Object
131 132 133 134 |
# File 'lib/closure_tree/support_attributes.rb', line 131 def order_column_sym require_order_column order_column.to_sym end |
#parent_column_name ⇒ Object
55 56 57 |
# File 'lib/closure_tree/support_attributes.rb', line 55 def parent_column_name [:parent_column_name] end |
#parent_column_sym ⇒ Object
59 60 61 |
# File 'lib/closure_tree/support_attributes.rb', line 59 def parent_column_sym parent_column_name.to_sym end |
#primary_key_column ⇒ Object
47 48 49 |
# File 'lib/closure_tree/support_attributes.rb', line 47 def primary_key_column model_class.columns.detect { |ea| ea.name == model_class.primary_key } end |
#primary_key_type ⇒ Object
51 52 53 |
# File 'lib/closure_tree/support_attributes.rb', line 51 def primary_key_type primary_key_column.type end |
#quoted_hierarchy_table_name ⇒ Object
82 83 84 |
# File 'lib/closure_tree/support_attributes.rb', line 82 def quoted_hierarchy_table_name connection.quote_table_name hierarchy_table_name end |
#quoted_id_column_name ⇒ Object
86 87 88 |
# File 'lib/closure_tree/support_attributes.rb', line 86 def quoted_id_column_name connection.quote_column_name model_class.primary_key end |
#quoted_name_column ⇒ Object
94 95 96 |
# File 'lib/closure_tree/support_attributes.rb', line 94 def quoted_name_column connection.quote_column_name name_column end |
#quoted_order_column(include_table_name = true) ⇒ Object
136 137 138 139 140 |
# File 'lib/closure_tree/support_attributes.rb', line 136 def quoted_order_column(include_table_name = true) require_order_column prefix = include_table_name ? "#{quoted_table_name}." : '' "#{prefix}#{connection.quote_column_name(order_column)}" end |
#quoted_parent_column_name ⇒ Object
90 91 92 |
# File 'lib/closure_tree/support_attributes.rb', line 90 def quoted_parent_column_name connection.quote_column_name parent_column_name end |
#quoted_table_name ⇒ Object
35 36 37 |
# File 'lib/closure_tree/support_attributes.rb', line 35 def quoted_table_name connection.quote_table_name(table_name) end |
#quoted_value(value) ⇒ Object
39 40 41 |
# File 'lib/closure_tree/support_attributes.rb', line 39 def quoted_value(value) value.is_a?(Numeric) ? value : quote(value) end |
#require_order_column ⇒ Object
127 128 129 |
# File 'lib/closure_tree/support_attributes.rb', line 127 def require_order_column raise ":order value, '#{[:order]}', isn't a column" if order_column.nil? end |
#short_hierarchy_class_name ⇒ String
Returns the constant name of the hierarchy_class
78 79 80 |
# File 'lib/closure_tree/support_attributes.rb', line 78 def short_hierarchy_class_name hierarchy_class_name.split('::').last end |
#t_alias_keyword ⇒ Object
table_name alias keyword , like “AS”. When used on table name alias, Oracle Database don’t support used ‘AS’
143 144 145 |
# File 'lib/closure_tree/support_attributes.rb', line 143 def t_alias_keyword ActiveRecord::Base.connection.adapter_name.to_sym == :OracleEnhanced ? '' : 'AS' end |