Module: SuperAuth::Nestable::ClassMethods

Defined in:
lib/super_auth/nestable.rb

Instance Method Summary collapse

Instance Method Details

#base_name_path(base = self) ⇒ Object



128
129
130
# File 'lib/super_auth/nestable.rb', line 128

def base_name_path(base = self)
  "#{singularize(base)}_name_path".to_sym
end

#base_path(base = self) ⇒ Object



124
125
126
# File 'lib/super_auth/nestable.rb', line 124

def base_path(base = self)
  "#{singularize(base)}_path".to_sym
end

#cte(id = nil, direction = :desc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/super_auth/nestable.rb', line 28

def cte(id = nil, direction = :desc)
  model = self
  cte_name = model.cte_name
  base_ds = model.select_all(pluralize)

  case direction
  when :asc
    base_ds = base_ds.where(id: id)

    recursive_ds = model
      .join(cte_name, parent_id: :id)
      .select_all(pluralize)
    base_ds, recursive_ds = with_ascending_paths(base_ds, recursive_ds, cte_name)
  when :desc
    if id
      base_ds = base_ds.where(id: id)
    else
      base_ds = base_ds.where(parent_id: id)
    end

    recursive_ds = model
      .join(cte_name, id: :parent_id)
      .select_all(pluralize(model))

    base_ds, recursive_ds = with_descending_paths(base_ds, recursive_ds, cte_name)
  end

  model.from(cte_name)
    .with_recursive(cte_name, base_ds, recursive_ds)
end

#cte_name(base = self) ⇒ Object



120
121
122
# File 'lib/super_auth/nestable.rb', line 120

def cte_name(base = self)
  "#{pluralize(base)}_cte".to_sym
end

#demodularize(base = self) ⇒ Object

See: ActiveSupport::Inflector.demodulize



104
105
106
107
108
109
110
# File 'lib/super_auth/nestable.rb', line 104

def demodularize(base = self)
  if i = base.name.rindex("::")
    base.name[(i + 2), base.name.length]
  else
    base.name
  end
end

#pluralize(base = self) ⇒ Object



112
113
114
# File 'lib/super_auth/nestable.rb', line 112

def pluralize(base = self)
  "#{demodularize(base).downcase}s".to_sym
end

#singularize(base = self) ⇒ Object



116
117
118
# File 'lib/super_auth/nestable.rb', line 116

def singularize(base = self)
  demodularize(base).downcase.to_sym
end

#with_ascending_paths(base_ds, recursive_ds, cte_name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/super_auth/nestable.rb', line 84

def with_ascending_paths(base_ds, recursive_ds, cte_name)
  [
    base_ds.select_append(Sequel.function(:cast, Sequel[table_name][:id].as(:text)).as(base_path)).select_append(Sequel[table_name][:name].as(:base_name_path)),
    recursive_ds.select_append(
      Sequel.function(:concat,
        Sequel.function(:cast, Sequel[table_name][:id].as(:text)),
        Sequel.lit("','"),
        Sequel.function(:cast, Sequel[cte_name][base_path].as(:text)),
      ).as(base_path)
    ).select_append(
       Sequel.function(:concat,
        Sequel[table_name][:name],
        Sequel.lit("','"),
        Sequel[cte_name][base_name_path],
      ).as(base_name_path)
    )
  ]
end

#with_descending_paths(base_ds, recursive_ds, cte_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/super_auth/nestable.rb', line 59

def with_descending_paths(base_ds, recursive_ds, cte_name)
  [
    base_ds.select_append(
      Sequel.function(
        :cast,
        Sequel[table_name][:id].as(:text)
      ).as(base_path)
    ).select_append(Sequel[table_name][:name].as(base_name_path)),

    recursive_ds.select_append(
      Sequel.function(:concat,
        Sequel.function(:cast, Sequel[cte_name][base_path].as(:text)),
        Sequel.lit("','"),
        Sequel.function(:cast, Sequel[pluralize][:id].as(:text)),
      ).as(base_path)
    ).select_append(
       Sequel.function(:concat,
        Sequel[cte_name][base_name_path],
        Sequel.lit("','"),
        Sequel[table_name][:name],
      ).as(base_name_path)
    )
  ]
end