Sha256: 1d14b387e1c0220e09d00e78d4e736b8fc512727b3f4c8eb425c06ef73c61d33

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

module Bhf
  module ActiveRecord

    module Object
      def to_bhf_s
        return title if self.respond_to? :title
        return name if self.respond_to? :name

        if self.respond_to? :attributes
          return title if attributes['title']
          return name if attributes['name']
          return "#{self.class.to_s.humanize} ID: #{send(self.class.primary_key)}" if attributes[self.class.primary_key]
        end

        self.to_s.humanize
      end
    end

    module Self
      def bhf_default_search(search_params)
        return if (search_term = search_params[:text]).blank?
        where_statement = []
        columns_hash.each_pair do |name, props|
          is_number = search_term.to_i.to_s == search_term || search_term.to_f.to_s == search_term

          if props.type == :string || props.type == :text
            where_statement << "#{name} LIKE '%#{search_term}%'"
          elsif props.type == :integer && is_number
            where_statement << "#{name} = #{search_term.to_i}"
          elsif props.type == :float && is_number
            where_statement << "#{name} = #{search_term.to_f}"
          end
        end

        where_statement.join(' OR ')
      end
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bhf-0.2.7 lib/bhf/active_record.rb
bhf-0.2.6 lib/bhf/active_record.rb
bhf-0.2.5 lib/bhf/active_record.rb
bhf-0.2.4 lib/bhf/active_record.rb