Module: Effective::Resources::Sql
- Included in:
- Effective::Resource
- Defined in:
- app/models/effective/resources/sql.rb
Instance Method Summary collapse
- #column(name) ⇒ Object
- #column_names ⇒ Object
- #columns ⇒ Object
- #max_id ⇒ Object
-
#search_columns ⇒ Object
Any string or text columns TODO: filter out _type columns for polymorphic.
-
#sort_column ⇒ Object
This tries to figure out the column we should order this collection by.
- #sql_column(name) ⇒ Object
- #sql_direction(name) ⇒ Object
-
#sql_type(name) ⇒ Object
This is for EffectiveDatatables (col as:).
- #table ⇒ Object
Instance Method Details
#column(name) ⇒ Object
5 6 7 8 |
# File 'app/models/effective/resources/sql.rb', line 5 def column(name) name = name.to_s columns.find { |col| col.name == name || (belongs_to(name) && col.name == belongs_to(name).foreign_key) } end |
#column_names ⇒ Object
14 15 16 |
# File 'app/models/effective/resources/sql.rb', line 14 def column_names @column_names ||= columns.map { |col| col.name } end |
#columns ⇒ Object
10 11 12 |
# File 'app/models/effective/resources/sql.rb', line 10 def columns klass.columns end |
#max_id ⇒ Object
22 23 24 25 |
# File 'app/models/effective/resources/sql.rb', line 22 def max_id return 999999 unless klass.respond_to?(:unscoped) @max_id ||= klass.unscoped.maximum(klass.primary_key).to_i end |
#search_columns ⇒ Object
Any string or text columns TODO: filter out _type columns for polymorphic
79 80 81 |
# File 'app/models/effective/resources/sql.rb', line 79 def search_columns columns.map { |column| column.name if [:string, :text].include?(column.type) }.compact end |
#sort_column ⇒ Object
This tries to figure out the column we should order this collection by. Whatever would match up with the .to_s
69 70 71 72 73 74 75 |
# File 'app/models/effective/resources/sql.rb', line 69 def sort_column ['name', 'title', 'label', 'subject', 'full_name', 'first_name', 'email', 'description'].each do |name| return name if column_names.include?(name) end klass.primary_key end |
#sql_column(name) ⇒ Object
27 28 29 30 31 32 |
# File 'app/models/effective/resources/sql.rb', line 27 def sql_column(name) column = column(name) return nil unless table && column [klass.connection.quote_table_name(table.name), klass.connection.quote_column_name(column.name)].join('.') end |
#sql_direction(name) ⇒ Object
34 35 36 |
# File 'app/models/effective/resources/sql.rb', line 34 def sql_direction(name) name.to_s.downcase == 'desc' ? 'DESC' : 'ASC' end |
#sql_type(name) ⇒ Object
This is for EffectiveDatatables (col as:)
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 |
# File 'app/models/effective/resources/sql.rb', line 39 def sql_type(name) name = name.to_s if belongs_to_polymorphic(name) :belongs_to_polymorphic elsif belongs_to(name) :belongs_to elsif has_and_belongs_to_many(name) :has_and_belongs_to_many elsif has_many(name) :has_many elsif has_one(name) :has_one elsif name == 'id' && defined?(EffectiveObfuscation) && klass.respond_to?(:deobfuscate) :effective_obfuscation elsif name == 'roles' && defined?(EffectiveRoles) && klass.respond_to?(:with_role) :effective_roles elsif (name.include?('_address') || name.include?('_addresses')) && defined?(EffectiveAddresses) && (klass.new rescue nil).respond_to?(:effective_addresses) :effective_addresses elsif (column = column(name)) column.type elsif name.ends_with?('_id') :integer else :string end end |
#table ⇒ Object
18 19 20 |
# File 'app/models/effective/resources/sql.rb', line 18 def table klass.unscoped.table end |