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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/sorcery/couchbase_model.rb', line 4
def self.included(klass)
klass.class_eval do
class << self
alias :orig_authenticates_with_sorcery! :authenticates_with_sorcery!
def authenticates_with_sorcery!
orig_authenticates_with_sorcery!
init_couchbase_support! if defined?(Couchbase) && self.ancestors.include?(Couchbase::Model)
end
protected
def init_couchbase_support!
self.class_eval do
sorcery_config.username_attribute_names.each do |username|
attribute username
end
attribute sorcery_config.email_attribute_name unless sorcery_config.username_attribute_names.include?(sorcery_config.email_attribute_name)
attribute sorcery_config.crypted_password_attribute_name
attribute sorcery_config.salt_attribute_name
attribute sorcery_config.activation_token_attribute_name if sorcery_config.respond_to? :activation_token_attribute_name
attribute sorcery_config.activation_state_attribute_name if sorcery_config.respond_to? :activation_token_attribute_name
attribute sorcery_config.remember_me_token_attribute_name if sorcery_config.respond_to? :remember_me_token_attribute_name
if sorcery_config.respond_to? :remember_me_token_expires_at_attribute_name
attribute sorcery_config.remember_me_token_expires_at_attribute_name
define_method sorcery_config.remember_me_token_expires_at_attribute_name do
time = read_attribute(sorcery_config.remember_me_token_expires_at_attribute_name)
case time
when Array
Time.utc *time
when String
Time.new time
else
time
end
end
end
view :all
view *_sorcery_view_attributes.map { |attr| "by_#{attr}" }
ensure_sorcery_design_document!
end
end
def ensure_sorcery_design_document!
bucket.save_design_doc(_sorcery_design_doc)
end
def _sorcery_design_doc
doc = {
'_id' => "_design/#{design_document}",
'language' => 'javascript',
'views' => {
'all' => {
'map' => <<-JS
function (doc, meta) {
if (doc.type && doc.type == '#{design_document}')
emit(meta.id, null);
}
JS
}
}
}
_sorcery_view_attributes.each do |attribute|
doc['views']["by_#{attribute}"] = {
'map' => <<-JS
function (doc, meta) {
if (doc.type && doc.type == '#{design_document}')
emit(doc.#{attribute}, null);
}
JS
}
end
doc
end
def _sorcery_view_attributes
attributes = sorcery_config.username_attribute_names
attributes << sorcery_config.activation_token_attribute_name if sorcery_config.respond_to? :activation_token_attribute_name
attributes << sorcery_config.remember_me_token_attribute_name if sorcery_config.respond_to? :remember_me_token_attribute_name
attributes << sorcery_config.email_attribute_name
attributes.uniq
end
end
end
end
|