Module: ClosureTree::SupportAttributes

Extended by:
Forwardable
Included in:
Support
Defined in:
lib/closure_tree/support_attributes.rb

Instance Method Summary collapse

Instance Method Details

#advisory_lock_nameObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/closure_tree/support_attributes.rb', line 11

def advisory_lock_name
  # Allow customization via options or instance method
  if options[:advisory_lock_name]
    case options[:advisory_lock_name]
    when Proc
      # Allow dynamic generation via proc
      options[:advisory_lock_name].call(base_class)
    when Symbol
      # Allow delegation to a model method
      if model_class.respond_to?(options[:advisory_lock_name])
        model_class.send(options[:advisory_lock_name])
      else
        raise ArgumentError, "Model #{model_class} does not respond to #{options[:advisory_lock_name]}"
      end
    else
      # Use static string value
      options[:advisory_lock_name].to_s
    end
  else
    # Default: Use CRC32 for a shorter, consistent hash
    # This gives us 8 hex characters which is plenty for uniqueness
    # and leaves room for prefixes
    "ct_#{Zlib.crc32(base_class.name.to_s).to_s(16)}"
  end
end

#dont_order_rootsObject



104
105
106
# File 'lib/closure_tree/support_attributes.rb', line 104

def dont_order_roots
  options[:dont_order_roots] || false
end

#hierarchy_class_nameObject



45
46
47
# File 'lib/closure_tree/support_attributes.rb', line 45

def hierarchy_class_name
  options[:hierarchy_class_name] || "#{model_class}Hierarchy"
end

#name_columnObject



65
66
67
# File 'lib/closure_tree/support_attributes.rb', line 65

def name_column
  options[:name_column]
end

#name_symObject



69
70
71
# File 'lib/closure_tree/support_attributes.rb', line 69

def name_sym
  name_column.to_sym
end

#nulls_last_order_byObject



108
109
110
# File 'lib/closure_tree/support_attributes.rb', line 108

def nulls_last_order_by
  Arel.sql "-#{quoted_order_column} #{order_by_order(true)}"
end

#order_byObject



100
101
102
# File 'lib/closure_tree/support_attributes.rb', line 100

def order_by
  options[:order]
end

#order_by_order(reverse = false) ⇒ Object



112
113
114
115
116
# File 'lib/closure_tree/support_attributes.rb', line 112

def order_by_order(reverse = false)
  desc = !(order_by.to_s =~ /DESC\z/).nil?
  desc = !desc if reverse
  desc ? 'DESC' : 'ASC'
end

#order_columnObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/closure_tree/support_attributes.rb', line 118

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_symObject



133
134
135
136
# File 'lib/closure_tree/support_attributes.rb', line 133

def order_column_sym
  require_order_column
  order_column.to_sym
end

#parent_column_nameObject



57
58
59
# File 'lib/closure_tree/support_attributes.rb', line 57

def parent_column_name
  options[:parent_column_name]
end

#parent_column_symObject



61
62
63
# File 'lib/closure_tree/support_attributes.rb', line 61

def parent_column_sym
  parent_column_name.to_sym
end

#primary_key_columnObject



49
50
51
# File 'lib/closure_tree/support_attributes.rb', line 49

def primary_key_column
  model_class.columns.detect { |ea| ea.name == model_class.primary_key }
end

#primary_key_typeObject



53
54
55
# File 'lib/closure_tree/support_attributes.rb', line 53

def primary_key_type
  primary_key_column.type
end

#quoted_hierarchy_table_nameObject



84
85
86
# File 'lib/closure_tree/support_attributes.rb', line 84

def quoted_hierarchy_table_name
  connection.quote_table_name hierarchy_table_name
end

#quoted_id_column_nameObject



88
89
90
# File 'lib/closure_tree/support_attributes.rb', line 88

def quoted_id_column_name
  connection.quote_column_name model_class.primary_key
end

#quoted_name_columnObject



96
97
98
# File 'lib/closure_tree/support_attributes.rb', line 96

def quoted_name_column
  connection.quote_column_name name_column
end

#quoted_order_column(include_table_name = true) ⇒ Object



138
139
140
141
142
# File 'lib/closure_tree/support_attributes.rb', line 138

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_nameObject



92
93
94
# File 'lib/closure_tree/support_attributes.rb', line 92

def quoted_parent_column_name
  connection.quote_column_name parent_column_name
end

#quoted_table_nameObject



37
38
39
# File 'lib/closure_tree/support_attributes.rb', line 37

def quoted_table_name
  connection.quote_table_name(table_name)
end

#quoted_value(value) ⇒ Object



41
42
43
# File 'lib/closure_tree/support_attributes.rb', line 41

def quoted_value(value)
  value.is_a?(Numeric) ? value : quote(value)
end

#require_order_columnObject



129
130
131
# File 'lib/closure_tree/support_attributes.rb', line 129

def require_order_column
  raise ":order value, '#{options[:order]}', isn't a column" if order_column.nil?
end

#short_hierarchy_class_nameString

Returns the constant name of the hierarchy_class

Examples:

Namespace::Model.hierarchy_class_name # => "Namespace::ModelHierarchy"
Namespace::Model.short_hierarchy_class_name # => "ModelHierarchy"

Returns:

  • (String)

    the constant name



80
81
82
# File 'lib/closure_tree/support_attributes.rb', line 80

def short_hierarchy_class_name
  hierarchy_class_name.split('::').last
end

#t_alias_keywordObject

table_name alias keyword , like “AS”. When used on table name alias, Oracle Database don’t support used ‘AS’



145
146
147
# File 'lib/closure_tree/support_attributes.rb', line 145

def t_alias_keyword
  ActiveRecord::Base.connection.adapter_name.to_sym == :OracleEnhanced ? '' : 'AS'
end