Class: TableWarnings::Column
- Inherits:
-
Object
- Object
- TableWarnings::Column
- Defined in:
- lib/table_warnings/column.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #association ⇒ Object
- #blanks?(conditions) ⇒ Boolean
-
#initialize(table, name) ⇒ Column
constructor
A new instance of Column.
- #max ⇒ Object
- #min ⇒ Object
-
#nonexistent_owners?(conditions) ⇒ Boolean
select zip_codes.* from zip_codes left join egrid_subregions on ‘egrid_subregions`.`abbreviation` = zip_codes.`egrid_subregion_abbreviation` where `egrid_subregions`.`abbreviation` is null t.project(’COUNT(*)‘).join(a_t, Arel::Nodes::OuterJoin).on(a_t.eq(t)).where(a_t.eq(nil)).
- #nulls?(conditions) ⇒ Boolean
- #string? ⇒ Boolean
- #values_outside?(min, max, conditions) ⇒ Boolean
Constructor Details
#initialize(table, name) ⇒ Column
Returns a new instance of Column.
6 7 8 9 |
# File 'lib/table_warnings/column.rb', line 6 def initialize(table, name) @table = table @name = name.to_s end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/table_warnings/column.rb', line 4 def name @name end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
3 4 5 |
# File 'lib/table_warnings/column.rb', line 3 def table @table end |
Instance Method Details
#association ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/table_warnings/column.rb', line 59 def association return @association.first if @association.is_a?(Array) # ruby needs an easy way to memoize things that might be false or nil @association = table.reflect_on_all_associations(:belongs_to).select do |assoc| assoc.foreign_key == name end if @association.many? raise ArgumentError, "More than one association on #{table.name} uses foreign key #{name.inspect}" end @association.first end |
#blanks?(conditions) ⇒ Boolean
19 20 21 |
# File 'lib/table_warnings/column.rb', line 19 def blanks?(conditions) table.where(conditions).where(["LENGTH(TRIM(#{table.quoted_table_name}.#{name})) = 0"]).count > 0 end |
#max ⇒ Object
41 42 43 |
# File 'lib/table_warnings/column.rb', line 41 def max table.maximum(name) end |
#min ⇒ Object
37 38 39 |
# File 'lib/table_warnings/column.rb', line 37 def min table.minimum(name) end |
#nonexistent_owners?(conditions) ⇒ Boolean
select zip_codes.* from zip_codes left join egrid_subregions on ‘egrid_subregions`.`abbreviation` = zip_codes.`egrid_subregion_abbreviation` where `egrid_subregions`.`abbreviation` is null t.project(’COUNT(*)‘).join(a_t, Arel::Nodes::OuterJoin).on(a_t.eq(t)).where(a_t.eq(nil))
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/table_warnings/column.rb', line 47 def nonexistent_owners?(conditions) relation = table.includes(association.name).where( table.arel_table[association.foreign_key].not_eq(nil).and( # not this query's job association.klass.arel_table[association.klass.primary_key].eq(nil)) # columns in the right table are set to NULL if they don't exist ) if conditions.empty? relation.count > 0 else relation.where(conditions).count > 0 end end |
#nulls?(conditions) ⇒ Boolean
11 12 13 |
# File 'lib/table_warnings/column.rb', line 11 def nulls?(conditions) table.where(conditions).where(name => nil).count > 0 end |
#string? ⇒ Boolean
15 16 17 |
# File 'lib/table_warnings/column.rb', line 15 def string? table.columns_hash[name].try(:type) == :string end |
#values_outside?(min, max, conditions) ⇒ Boolean
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/table_warnings/column.rb', line 23 def values_outside?(min, max, conditions) t = table.arel_table range_conditions = if min and max t[name].lt(min).or(t[name].gt(max)) elsif min t[name].lt(min) elsif max t[name].lt(max) else raise RuntimeError, "Either max or min or both should be defined" end table.where(conditions).where(range_conditions.and(t[name].not_eq(nil))).count > 0 end |