Sha256: 7512e88873a4c135c32445999062e5e1eddf6cf252d008371fd739abb0608313

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

module Bhf
  module ActiveRecord
    module Object
      extend ActiveSupport::Concern
      
      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

      module ClassMethods
        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
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
bhf-0.3.8 lib/bhf/active_record/active_record.rb
bhf-0.3.7 lib/bhf/active_record/active_record.rb
bhf-0.3.6.4 lib/bhf/active_record/active_record.rb
bhf-0.3.6.3 lib/bhf/active_record/active_record.rb
bhf-0.3.6.2 lib/bhf/active_record/active_record.rb
bhf-0.3.6.1 lib/bhf/active_record/active_record.rb
bhf-0.3.5 lib/bhf/active_record/active_record.rb
bhf-0.3.4 lib/bhf/active_record/active_record.rb
bhf-0.3.3 lib/bhf/active_record/active_record.rb
bhf-0.3.2 lib/bhf/active_record/active_record.rb
bhf-0.3.1 lib/bhf/active_record/active_record.rb
bhf-0.3.0 lib/bhf/active_record/active_record.rb
bhf-0.2.9 lib/bhf/active_record/active_record.rb
bhf-0.2.8 lib/bhf/active_record/active_record.rb