Sha256: 68f7bed82f5724f9133fe95023d2749603c4521fbbd274099a879508a377e6f4

Contents?: true

Size: 1.62 KB

Versions: 21

Compression:

Stored size: 1.62 KB

Contents

module Bhf::ActiveRecord
  module Base

    extend ActiveSupport::Concern

    def to_bhf_s
      return title if self.respond_to? :title
      return name if self.respond_to? :name
      return headline if self.respond_to? :headline

      if self.respond_to?(:attributes)
        return title if attributes['title']
        return name if attributes['name']
        klass_name = if self.class.respond_to?(:model_name)
          self.class.model_name.human
        else
          self.class.to_s.humanize
        end
        return "#{klass_name} 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 where([]) 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 << "LOWER(#{name}) LIKE LOWER('%#{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(where_statement.join(' OR '))
      end

      def bhf_attribute_method?(column_name)
        column_names.include?(column_name)
      end

      def bhf_primary_key
        primary_key
      end

      def bhf_embedded?
        false
      end
    end

  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
bhf-1.0.0.beta16 lib/bhf/active_record/base.rb
bhf-1.0.0.beta15 lib/bhf/active_record/base.rb
bhf-1.0.0.beta14 lib/bhf/active_record/base.rb
bhf-1.0.0.beta13 lib/bhf/active_record/base.rb
bhf-1.0.0.beta12 lib/bhf/active_record/base.rb
bhf-1.0.0.beta11 lib/bhf/active_record/base.rb
bhf-1.0.0.beta10 lib/bhf/active_record/base.rb
bhf-1.0.0.beta9 lib/bhf/active_record/base.rb
bhf-1.0.0.beta8 lib/bhf/active_record/base.rb
bhf-1.0.0.beta7 lib/bhf/active_record/base.rb
bhf-1.0.0.beta6 lib/bhf/active_record/base.rb
bhf-1.0.0.beta5 lib/bhf/active_record/base.rb
bhf-1.0.0.beta4 lib/bhf/active_record/base.rb
bhf-1.0.0.beta3 lib/bhf/active_record/base.rb
bhf-1.0.0.beta2 lib/bhf/active_record/base.rb
bhf-1.0.0.beta1 lib/bhf/active_record/base.rb
bhf-0.10.17 lib/bhf/active_record/base.rb
bhf-0.10.16 lib/bhf/active_record/base.rb
bhf-0.10.15 lib/bhf/active_record/base.rb
bhf-0.10.14 lib/bhf/active_record/base.rb