Class: AdminAssistant::ActiveRecordColumn::ConditionUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_assistant/active_record_column.rb

Instance Method Summary collapse

Constructor Details

#initialize(ar_query_condition, search, name, field_type) ⇒ ConditionUpdate

Returns a new instance of ConditionUpdate.



70
71
72
73
# File 'lib/admin_assistant/active_record_column.rb', line 70

def initialize(ar_query_condition, search, name, field_type)
  @ar_query_condition, @search, @name, @field_type =
      ar_query_condition, search, name, field_type
end

Instance Method Details

#add_blank_conditionObject



75
76
77
78
79
80
81
# File 'lib/admin_assistant/active_record_column.rb', line 75

def add_blank_condition
  @ar_query_condition.add_condition do |sub_cond|
    sub_cond.boolean_join = :or
    sub_cond.sqls << "#{table_name}.#{@name} is null"
    sub_cond.sqls << "#{table_name}.#{@name} = ''"
  end
end

#add_case_insensitive_string_comparisonObject



83
84
85
86
87
# File 'lib/admin_assistant/active_record_column.rb', line 83

def add_case_insensitive_string_comparison
  @ar_query_condition.sqls <<
      "LOWER(#{table_name}.#{@name}) like LOWER(?)"
  @ar_query_condition.bind_vars << "%#{value_for_query}%"
end

#add_comparative_conditionObject



89
90
91
92
# File 'lib/admin_assistant/active_record_column.rb', line 89

def add_comparative_condition
  @ar_query_condition.sqls << "#{table_name}.#{@name} #{comparator} ?"
  @ar_query_condition.bind_vars << value_for_query
end

#add_equality_conditionObject



94
95
96
97
# File 'lib/admin_assistant/active_record_column.rb', line 94

def add_equality_condition
  @ar_query_condition.sqls << "#{table_name}.#{@name} = ?"
  @ar_query_condition.bind_vars << value_for_query
end

#add_range_conditionObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/admin_assistant/active_record_column.rb', line 99

def add_range_condition
  if lower_limit = @search.send(@name)[:gt]
    @ar_query_condition.sqls << "#{table_name}.#{@name} > ?"
    @ar_query_condition.bind_vars << lower_limit
  end
  if upper_limit = @search.send(@name)[:lt]
    @ar_query_condition.sqls << "#{table_name}.#{@name} < ?"
    @ar_query_condition.bind_vars << upper_limit
  end
end

#column_is_a_string_type?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/admin_assistant/active_record_column.rb', line 118

def column_is_a_string_type?
  ![:boolean, :integer].include?(@field_type)
end

#comparatorObject



114
115
116
# File 'lib/admin_assistant/active_record_column.rb', line 114

def comparator
  @search.comparator @name
end

#compare_to_range?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/admin_assistant/active_record_column.rb', line 110

def compare_to_range?
  @search.compare_to_range? @name
end

#runObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/admin_assistant/active_record_column.rb', line 122

def run
  if @search.blank?(@name)
    add_blank_condition
  else
    unless value_for_query.nil?
      if comparator
        add_comparative_condition
      elsif compare_to_range?
        add_range_condition
      elsif column_is_a_string_type?
        add_case_insensitive_string_comparison
      else
        add_equality_condition
      end
    end
  end
end

#table_nameObject



140
141
142
# File 'lib/admin_assistant/active_record_column.rb', line 140

def table_name
  @search.model_class.table_name
end

#value_for_queryObject



144
145
146
# File 'lib/admin_assistant/active_record_column.rb', line 144

def value_for_query
  @search.send @name
end