Module: Effective::Resources::Relation
- Included in:
- Effective::Resource
- Defined in:
- app/models/effective/resources/relation.rb
Constant Summary collapse
- TARGET_LIST_LIMIT =
1500
- TARGET_KEYS_LIMIT =
30000
Instance Method Summary collapse
-
#order(name, direction = :asc, as: nil, sort: nil, sql_column: nil, limit: nil, reorder: false) ⇒ Object
name: sort by this column, or this relation sort: when a symbol or boolean, this is the relation’s column to sort by.
-
#relation ⇒ Object
This could be active_model? in which we just return the klass itself here This value ends up being crud_controller resource_scope().
- #search(name, value, as: nil, fuzzy: true, sql_column: nil) ⇒ Object
- #search_any(value, columns: nil, fuzzy: nil) ⇒ Object
Instance Method Details
#order(name, direction = :asc, as: nil, sort: nil, sql_column: nil, limit: nil, reorder: false) ⇒ Object
name: sort by this column, or this relation sort: when a symbol or boolean, this is the relation’s column to sort by
21 22 23 24 25 26 27 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 58 59 60 61 62 |
# File 'app/models/effective/resources/relation.rb', line 21 def order(name, direction = :asc, as: nil, sort: nil, sql_column: nil, limit: nil, reorder: false) raise 'expected relation to be present' unless relation sql_column ||= sql_column(name) sql_type = (as || sql_type(name)) association = associated(name) sql_direction = sql_direction(direction) @relation = relation.reorder(nil) if reorder case sql_type when :belongs_to relation .order(order_by_associated_conditions(association, sort: sort, direction: direction, limit: limit)) when :belongs_to_polymorphic relation .order(Arel.sql("#{sql_column}_type #{sql_direction}")) .order(Arel.sql("#{sql_column}_id #{sql_direction}")) when :has_and_belongs_to_many, :has_many, :has_one relation .order(order_by_associated_conditions(association, sort: sort, direction: direction, limit: limit)) .order(Arel.sql("#{sql_column(klass.primary_key)} #{sql_direction}")) when :effective_addresses relation .order(order_by_associated_conditions(associated(:addresses), sort: sort, direction: direction, limit: limit)) .order(Arel.sql("#{sql_column(klass.primary_key)} #{sql_direction}")) when :active_storage relation.send("with_attached_#{name}").references("#{name}_attachment") .order(Arel.sql("active_storage_blobs.filename #{sql_direction}")) when :effective_roles relation.order(Arel.sql("#{sql_column(:roles_mask)} #{sql_direction}")) when :string, :text relation .order(Arel.sql(("ISNULL(#{sql_column}), " if mysql?).to_s + "#{sql_column}='' ASC, #{sql_column} #{sql_direction}" + (" NULLS LAST" if postgres?).to_s)) when :time relation .order(Arel.sql(("ISNULL(#{sql_column}), " if mysql?).to_s + "EXTRACT(hour from #{sql_column}) #{sql_direction}, EXTRACT(minute from #{sql_column}) #{sql_direction}" + (" NULLS LAST" if postgres?).to_s)) else relation .order(Arel.sql(("ISNULL(#{sql_column}), " if mysql?).to_s + "#{sql_column} #{sql_direction}" + (" NULLS LAST" if postgres?).to_s)) end end |
#relation ⇒ Object
This could be active_model? in which we just return the klass itself here This value ends up being crud_controller resource_scope()
11 12 13 |
# File 'app/models/effective/resources/relation.rb', line 11 def relation @relation ||= (klass.respond_to?(:where) ? klass.where(nil) : klass) end |
#search(name, value, as: nil, fuzzy: true, sql_column: nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/effective/resources/relation.rb', line 64 def search(name, value, as: nil, fuzzy: true, sql_column: nil) raise 'expected relation to be present' unless relation sql_column ||= sql_column(name) sql_type = (as || sql_type(name)) fuzzy = true unless fuzzy == false if ['SUM(', 'COUNT(', 'MAX(', 'MIN(', 'AVG('].any? { |str| sql_column.to_s.include?(str) } return relation.having("#{sql_column} = ?", value) end association = associated(name) term = Effective::Attribute.new(sql_type, klass: (association.try(:klass) rescue nil) || klass).parse(value, name: name) # term == 'nil' rescue false is a Rails 4.1 fix, where you can't compare a TimeWithZone to 'nil' if (term == 'nil' rescue false) && ![:has_and_belongs_to_many, :has_many, :has_one, :belongs_to, :belongs_to_polymorphic, :effective_roles].include?(sql_type) return relation.where(is_null(sql_column)) end case sql_type when :belongs_to if term == 'nil' relation.where(is_null(association.foreign_key)) else relation.where(search_by_associated_conditions(association, term, fuzzy: fuzzy)) end when :belongs_to_polymorphic (type, id) = term.split('_') if term == 'nil' relation.where(is_null("#{sql_column}_id")).where(is_null("#{sql_column}_type")) elsif type.present? && id.present? # This was from a polymorphic select relation.where("#{sql_column}_id = ?", id).where("#{sql_column}_type = ?", type) elsif name == :user # Polymorphic user relation.where(search_by_associated_conditions(association, term, fuzzy: fuzzy)) else # Maybe from a string field collection = relation.none relation.unscoped.distinct("#{name}_type").pluck("#{name}_type").each do |klass_name| next if klass_name.nil? resource = Effective::Resource.new(klass_name) next unless resource.klass.present? collection = collection.or(relation.where("#{name}_id": resource.search_any(term), "#{name}_type": klass_name)) end collection end when :has_and_belongs_to_many, :has_many, :has_one relation.where(search_by_associated_conditions(association, term, fuzzy: fuzzy)) when :effective_addresses relation.where(id: Effective::Resource.new(association).search_any(value, fuzzy: fuzzy).pluck(:addressable_id)) when :effective_obfuscation # If value == term, it's an invalid deobfuscated id relation.where("#{sql_column} = ?", (value == term ? 0 : term)) when :effective_roles relation.with_role(term) when :active_storage relation.send("with_attached_#{name}").references("#{name}_attachment") .where(ActiveStorage::Blob.arel_table[:filename].matches("%#{term}%")) when :boolean relation.where("#{sql_column} = ?", term) when :datetime, :date end_at = ( case (value.to_s.scan(/(\d+)/).flatten).length when 1 ; term.end_of_year # Year when 2 ; term.end_of_month # Year-Month when 3 ; term.end_of_day # Year-Month-Day when 4 ; term.end_of_hour # Year-Month-Day Hour when 5 ; term.end_of_minute # Year-Month-Day Hour-Minute when 6 ; term + 1.second # Year-Month-Day Hour-Minute-Second else term end ) relation.where("#{sql_column} >= ? AND #{sql_column} <= ?", term, end_at) when :time timed = relation.where("EXTRACT(hour from #{sql_column}) = ?", term.utc.hour) timed = timed.where("EXTRACT(minute from #{sql_column}) = ?", term.utc.min) if term.min > 0 timed when :decimal, :currency if fuzzy && (term.round(0) == term) && value.to_s.include?('.') == false if term < 0 relation.where("#{sql_column} <= ? AND #{sql_column} > ?", term, term-1.0) else relation.where("#{sql_column} >= ? AND #{sql_column} < ?", term, term+1.0) end else relation.where("#{sql_column} = ?", term) end when :duration if fuzzy && (term % 60 == 0) && value.to_s.include?('m') == false if term < 0 relation.where("#{sql_column} <= ? AND #{sql_column} > ?", term, term-60) else relation.where("#{sql_column} >= ? AND #{sql_column} < ?", term, term+60) end else relation.where("#{sql_column} = ?", term) end when :integer relation.where("#{sql_column} = ?", term) when :percent relation.where("#{sql_column} = ?", term) when :price relation.where("#{sql_column} = ?", term) when :string, :text, :email if fuzzy relation.where("#{sql_column} #{ilike} ?", "%#{term}%") else relation.where("#{sql_column} = ?", term) end when :uuid if fuzzy relation.where("#{sql_column}::text #{ilike} ?", "%#{term}%") else relation.where("#{sql_column}::text = ?", term) end else raise "unsupported sql type #{sql_type}" end end |
#search_any(value, columns: nil, fuzzy: nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'app/models/effective/resources/relation.rb', line 188 def search_any(value, columns: nil, fuzzy: nil) raise 'expected relation to be present' unless relation # Assume this is a set of IDs if value.kind_of?(Integer) || value.kind_of?(Array) || (value.to_i.to_s == value) return relation.where(klass.primary_key => value) end # If the value is 3-something-like-this if (values = value.to_s.split('-')).length > 0 && (maybe_id = values.first).present? return relation.where(klass.primary_key => maybe_id) if (maybe_id.to_i.to_s == maybe_id) end # Otherwise, we fall back to a string/text search of all columns columns = Array(columns || search_columns) fuzzy = true unless fuzzy == false conditions = ( if fuzzy columns.map { |name| "#{sql_column(name)} #{ilike} :fuzzy" } else columns.map { |name| "#{sql_column(name)} = :value" } end ).join(' OR ') relation.where(conditions, fuzzy: "%#{value}%", value: value) end |