Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 68 lines (61 sloc) 1.902 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
module Devise
  module Orm
    module CouchRestModel
      module Hook
        def devise_modules_hook!
          extend Schema
          create_views_by_authentication_keys
          yield
          return unless Devise.apply_schema
          devise_modules.each { |m| send(m) if respond_to?(m, true) }
        end

        private
        def create_views_by_authentication_keys
          authentication_keys.each do |key_name|
            view_by key_name
          end
          view_by :remember_token
        end
      end

      module Schema
        include Devise::Schema
        # Tell how to apply schema methods.
        def apply_devise_schema(name, type, options={})
          return unless Devise.apply_schema
          type = String if name == :sign_in_count
          type = Time if type == DateTime
          if type != String
            property name, {:cast_as => type}.merge(options)
          else
            property name
          end
        end

        def find_for_authentication(conditions)
          find(:conditions => conditions)
        end

        def find(*args)
          options = args.extract_options!

          if options.present?
            raise "You can't search with more than one condition yet =(" if options[:conditions].keys.size > 1
            find_one_by_key_and_value(options[:conditions].keys.first, options[:conditions].values.first)
          else
            id = args.flatten.compact.uniq.to_s
            find_one_by_key_and_value(:id, id)
          end
        end

        private
        def find_one_by_key_and_value(key, value)
          if key == :id
            get(value)
          else
            send("by_#{key}", {:key => value, :limit => 1}).first
          end
        end
      end
    end
  end
end

CouchRest::Model::Base.class_eval do
  extend Devise::Models
  extend Devise::Orm::CouchRestModel::Hook
end
Something went wrong with that request. Please try again.