Module: SuperAuth::Nestable::ClassMethods
- Defined in:
- lib/super_auth/nestable.rb
Instance Method Summary collapse
- #base_name_path(base = self) ⇒ Object
- #base_path(base = self) ⇒ Object
- #cte(id = nil, direction = :desc) ⇒ Object
- #cte_name(base = self) ⇒ Object
-
#demodularize(base = self) ⇒ Object
See: ActiveSupport::Inflector.demodulize.
- #pluralize(base = self) ⇒ Object
- #singularize(base = self) ⇒ Object
-
#string_cast_type ⇒ Object
Helper method to get the appropriate string cast type for the database.
- #with_ascending_paths(base_ds, recursive_ds, cte_name) ⇒ Object
- #with_descending_paths(base_ds, recursive_ds, cte_name) ⇒ Object
Instance Method Details
#base_name_path(base = self) ⇒ Object
135 136 137 |
# File 'lib/super_auth/nestable.rb', line 135 def base_name_path(base = self) "#{singularize(base)}_name_path".to_sym end |
#base_path(base = self) ⇒ Object
131 132 133 |
# File 'lib/super_auth/nestable.rb', line 131 def base_path(base = self) "#{singularize(base)}_path".to_sym end |
#cte(id = nil, direction = :desc) ⇒ Object
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 |
# File 'lib/super_auth/nestable.rb', line 38 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
127 128 129 |
# File 'lib/super_auth/nestable.rb', line 127 def cte_name(base = self) "super_auth_#{pluralize(base)}_cte".to_sym end |
#demodularize(base = self) ⇒ Object
See: ActiveSupport::Inflector.demodulize
111 112 113 114 115 116 117 |
# File 'lib/super_auth/nestable.rb', line 111 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
119 120 121 |
# File 'lib/super_auth/nestable.rb', line 119 def pluralize(base = self) "super_auth_#{demodularize(base).downcase}s".to_sym end |
#singularize(base = self) ⇒ Object
123 124 125 |
# File 'lib/super_auth/nestable.rb', line 123 def singularize(base = self) demodularize(base).downcase.to_sym end |
#string_cast_type ⇒ Object
Helper method to get the appropriate string cast type for the database
29 30 31 32 33 34 35 36 |
# File 'lib/super_auth/nestable.rb', line 29 def string_cast_type case SuperAuth.db.database_type when :mysql, :mysql2 :char else :text end end |
#with_ascending_paths(base_ds, recursive_ds, cte_name) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/super_auth/nestable.rb', line 91 def with_ascending_paths(base_ds, recursive_ds, cte_name) [ base_ds.select_append(Sequel[table_name][:id].cast(string_cast_type).as(base_path)).select_append(Sequel[table_name][:name].as(base_name_path)), recursive_ds.select_append( Sequel.function(:concat, Sequel[table_name][:id].cast(string_cast_type), Sequel.lit("','"), Sequel[cte_name][base_path].cast(string_cast_type), ).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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/super_auth/nestable.rb', line 69 def with_descending_paths(base_ds, recursive_ds, cte_name) [ base_ds.select_append( Sequel[table_name][:id].cast(string_cast_type).as(base_path) ).select_append(Sequel[table_name][:name].as(base_name_path)), recursive_ds.select_append( Sequel.function(:concat, Sequel[cte_name][base_path].cast(string_cast_type), Sequel.lit("','"), Sequel[pluralize][:id].cast(string_cast_type), ).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 |