class Operation < ActiveRecord::Base
   has_many :active_data
   belongs_to :operation_type
   belongs_to :operation_source
   
   $active_db=Rails.configuration.database_configuration[Rails.env]["adapter"]
   def self.like_compare(object,compared,invert)
     if $active_db == "sqlserver"
       if invert == 0;sign="LIKE";else;sign="NOT LIKE";end
       result=self.where("lower(#{object}) #{sign} lower('#{compared}')")
     else
       if invert == 0;sign="=";else;sign="!=";end
       result=self.where("lower(#{object}) LIKE lower('#{compared}') #{sign} true")
     end
     return result
   end
   def self.global_filter(id)
       @filter=GlobalFilter.where(:user_id => id).first
       @source_id=@filter.operation_source_id
       @filter_id=@filter.custom_filter_id
       @result=self
       if @source_id == 0
           @result=@result.all
       else
           @result=@result.where(:operation_source_id => @source_id)
       end
       if @filter_id == 0
           @result=@result.all
       else
           @result=@result\
           .where(:id => OperationFilter.where(:custom_filter_id => @filter_id).select(:operation_id))\
       end 
       return @result
   end
   def self.operation_name(output_name)
    result=self.select(
           "(CASE WHEN display_name IS NULL OR display_name = '' THEN \"operations\".\"name\" ELSE display_name END) as #{output_name}"
       )
    return result
   end
   def self.operation_name_group
       result=self.group(
              "(CASE WHEN display_name IS NULL OR display_name = '' THEN \"operations\".\"name\" ELSE display_name END)"
          )
       return result
   end
end
