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
- #ilike ⇒ Object
- #is_null(sql_column) ⇒ Object
- #max_id ⇒ Object
- #mysql? ⇒ Boolean
- #postgres? ⇒ Boolean
-
#search_columns ⇒ Object
Any string or text columns TODO: filter out _type columns for polymorphic.
- #search_columns=(name) ⇒ Object
-
#sort_column ⇒ Object
This tries to figure out the column we should order this collection by.
- #sort_column=(name) ⇒ Object
- #sql_column(name) ⇒ Object
- #sql_direction(name) ⇒ Object
-
#sql_type(name) ⇒ Object
This is for EffectiveDatatables (col as:) Might be :name, or ‘users.name’.
- #table ⇒ Object
Instance Method Details
#column(name) ⇒ Object
6 7 8 9 10 11 |
# File 'app/models/effective/resources/sql.rb', line 6 def column(name) name = name.to_s bt = belongs_to(name) columns.find { |col| col.name == name || (bt && col.name == bt.foreign_key) } end |
#column_names ⇒ Object
17 18 19 |
# File 'app/models/effective/resources/sql.rb', line 17 def column_names @column_names ||= columns.map { |col| col.name } end |
#columns ⇒ Object
13 14 15 |
# File 'app/models/effective/resources/sql.rb', line 13 def columns klass.respond_to?(:columns) ? klass.columns : [] end |
#ilike ⇒ Object
107 108 109 |
# File 'app/models/effective/resources/sql.rb', line 107 def ilike @ilike ||= (postgres? ? 'ILIKE' : 'LIKE') # Only Postgres supports ILIKE, Mysql and Sqlite3 use LIKE end |
#is_null(sql_column) ⇒ Object
119 120 121 |
# File 'app/models/effective/resources/sql.rb', line 119 def is_null(sql_column) mysql? == true ? "ISNULL(#{sql_column})" : "#{sql_column} IS NULL" end |
#max_id ⇒ Object
25 26 27 28 |
# File 'app/models/effective/resources/sql.rb', line 25 def max_id return 999999 unless klass.respond_to?(:unscoped) @max_id ||= klass.unscoped.maximum(klass.primary_key).to_i end |
#mysql? ⇒ Boolean
115 116 117 |
# File 'app/models/effective/resources/sql.rb', line 115 def mysql? klass.connection.adapter_name == 'MySQL' end |
#postgres? ⇒ Boolean
111 112 113 |
# File 'app/models/effective/resources/sql.rb', line 111 def postgres? klass.connection.adapter_name == 'PostgreSQL' end |
#search_columns ⇒ Object
Any string or text columns TODO: filter out _type columns for polymorphic
96 97 98 99 |
# File 'app/models/effective/resources/sql.rb', line 96 def search_columns return @_search_columns if @_search_columns columns.map { |column| column.name if [:string, :text].include?(column.type) }.compact end |
#search_columns=(name) ⇒ Object
101 102 103 104 105 |
# File 'app/models/effective/resources/sql.rb', line 101 def search_columns=(name) names = Array(name) names.each { |name| raise "unknown search column: #{name}" unless column_names.include?(name) } @_search_columns = names 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 Unless it’s set from outside by datatables…
79 80 81 82 83 84 85 86 87 |
# File 'app/models/effective/resources/sql.rb', line 79 def sort_column return @_sort_column if @_sort_column ['name', 'title', 'label', 'subject', 'full_name', 'first_name', 'email', 'number', 'description'].each do |name| return name if column_names.include?(name) end klass.primary_key end |
#sort_column=(name) ⇒ Object
89 90 91 92 |
# File 'app/models/effective/resources/sql.rb', line 89 def sort_column=(name) raise "unknown sort column: #{name}" unless column_names.include?(name) @_sort_column = name end |
#sql_column(name) ⇒ Object
30 31 32 33 34 35 |
# File 'app/models/effective/resources/sql.rb', line 30 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
37 38 39 |
# File 'app/models/effective/resources/sql.rb', line 37 def sql_direction(name) name.to_s.downcase == 'desc' ? 'DESC' : 'ASC' end |
#sql_type(name) ⇒ Object
This is for EffectiveDatatables (col as:) Might be :name, or ‘users.name’
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 'app/models/effective/resources/sql.rb', line 43 def sql_type(name) name = (name.kind_of?(String) ? name.split('.').first : name.to_s) return :belongs_to if belongs_to(name) # Skip using columns() cause we dont need to check for belongs_to column = columns.find { |col| col.name == name } if column.present? column.type elsif has_many(name) :has_many elsif has_one(name) :has_one elsif belongs_to_polymorphic(name) :belongs_to_polymorphic elsif has_and_belongs_to_many(name) :has_and_belongs_to_many elsif active_storage(name) :active_storage 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 name.ends_with?('_id') :integer else :string end end |
#table ⇒ Object
21 22 23 |
# File 'app/models/effective/resources/sql.rb', line 21 def table klass.unscoped.table end |