Sha256: b1d037ab33f7f66d028a3727ef64c33cfaaf3d489d41377e01456309f46a2587

Contents?: true

Size: 1.1 KB

Versions: 19

Compression:

Stored size: 1.1 KB

Contents

class AdminAssistant
  module Request
    class Autocomplete < Base
      def associated_class
        @associated_class ||= Module.const_get(
          underscored_assoc_class_name.camelize
        )
      end
      
      def call
        results = records.map { |record|
          {:id => record.id.to_s, :name => record.send(record_name_field)}
        }
        @controller.send(:render, :json => results.to_json)
      end
      
      def record_name_field
        AssociationTarget.new(associated_class).default_name_method
      end
      
      def records
        action =~ /autocomplete_(.*)/
        associated_class.find(
          :all,
          :conditions => [
            "LOWER(#{record_name_field}) like ?",
            "%#{search_string.downcase unless search_string.nil?}%"
          ],
          :limit => 10,
          :order => "length(#{record_name_field}), lower(#{record_name_field})"
        )
      end
      
      def search_string
        @controller.params['q']
      end
      
      def underscored_assoc_class_name
        action =~ /autocomplete_(.*)/
        $1
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
admin_assistant-2.4.0 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.3.2 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.3.1 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.3.0 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.8 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.7 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.6 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.5 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.4 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.3 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.2 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.1 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.2.0 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.1.1 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.1.0 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.0.1 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.0.0 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.0.0.pre2 lib/admin_assistant/request/autocomplete.rb
admin_assistant-2.0.0.pre1 lib/admin_assistant/request/autocomplete.rb