Class: ActiveTree::Model

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Statusable
Defined in:
lib/active_tree/models/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Statusable

#active!, #active?, #inactive!, #inactive?, #set_default_status, #status?, #toggle_status!

Class Method Details

.owned_by(owner_id) ⇒ Object

Scoping by owner in order to select the partition



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_tree/models/model.rb', line 34

def self.owned_by(owner_id)
    # if we're looking for anything else but an integer, revert to the base class
    return self if !owner_id.is_a? Integer

    partition_suffix = "_#{owner_id}"

    #table = "#{ self.table_name }#{ partition_suffix }"
    table = ActiveTree::Store.new(owner_id).partition_name

    ApplicationRecord.connection.schema_cache.clear!
    return self if !ApplicationRecord.connection.schema_cache.data_source_exists? table

    # duplicate the class
    model_class = Class.new self
    original_class_name = self.name

    # ...for this owner
    class_name = "#{name}#{partition_suffix}"

    # specify the table
    model_class.define_singleton_method(:table_name) do
        table
    end

    # specify the name
    model_class.define_singleton_method(:name) do
        class_name
    end

  model_class.define_singleton_method(:sti_name) do
 original_class_name
  end

    # override the STI name lmfao
    model_class.define_singleton_method(:find_sti_class) do |p|
        original_class_name.constantize
    end

    # proceed
    model_class
end

.table_nameObject



9
10
11
12
# File 'lib/active_tree/models/model.rb', line 9

def self.table_name
    return ::ACTIVE_TREE_OPTIONS[:table_name] if defined? ::ACTIVE_TREE_OPTIONS
    return "active_tree_models"
end

Instance Method Details

#set_defaultsObject



24
25
26
27
28
# File 'lib/active_tree/models/model.rb', line 24

def set_defaults
    self.path ||= name.delete(" ").gsub(/[^0-9a-z ]/i, '') if name
    self.path_slug = path.parameterize if path
    self. ||= {}
end