loyal_passport 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +6 -0
  3. data/Rakefile +28 -0
  4. data/app/assets/javascripts/loyal_passport/admin/loyal_passport/assignments.js +2 -0
  5. data/app/assets/javascripts/loyal_passport/admin/loyal_passport/roles.js +2 -0
  6. data/app/assets/javascripts/loyal_passport/admin/users.js +2 -0
  7. data/app/assets/javascripts/loyal_passport/admin.js +0 -0
  8. data/app/assets/javascripts/loyal_passport/application.js +3 -0
  9. data/app/assets/javascripts/loyal_passport/login_colorbox.js +2 -0
  10. data/app/assets/stylesheets/loyal_passport/admin/loyal_passport/assignments.css +4 -0
  11. data/app/assets/stylesheets/loyal_passport/admin/loyal_passport/roles.css +4 -0
  12. data/app/assets/stylesheets/loyal_passport/admin/users.css +4 -0
  13. data/app/assets/stylesheets/loyal_passport/admin.css.scss +0 -0
  14. data/app/assets/stylesheets/loyal_passport/application.css.scss +278 -0
  15. data/app/assets/stylesheets/loyal_passport/login_colorbox.css.scss +0 -0
  16. data/app/assets/stylesheets/scaffold.css +56 -0
  17. data/app/controllers/loyal_passport/admin/loyal_passport/assignments_controller.rb +11 -0
  18. data/app/controllers/loyal_passport/admin/loyal_passport/roles_controller.rb +59 -0
  19. data/app/controllers/loyal_passport/admin/users_controller.rb +29 -0
  20. data/app/controllers/loyal_passport/application_controller.rb +9 -0
  21. data/app/controllers/loyal_passport/users/confirmations_controller.rb +6 -0
  22. data/app/controllers/loyal_passport/users/omniauth_callbacks_controller.rb +17 -0
  23. data/app/controllers/loyal_passport/users/passwords_controller.rb +8 -0
  24. data/app/controllers/loyal_passport/users/profile/informations_controller.rb +19 -0
  25. data/app/controllers/loyal_passport/users/registrations_controller.rb +71 -0
  26. data/app/controllers/loyal_passport/users/sessions_controller.rb +119 -0
  27. data/app/controllers/loyal_passport/users/unlocks_controller.rb +7 -0
  28. data/app/helpers/loyal_passport/admin/loyal_passport/assignments_helper.rb +5 -0
  29. data/app/helpers/loyal_passport/admin/loyal_passport/roles_helper.rb +5 -0
  30. data/app/helpers/loyal_passport/admin/users_helper.rb +5 -0
  31. data/app/helpers/loyal_passport/application_helper.rb +5 -0
  32. data/app/helpers/loyal_passport/users/confirmations_helper.rb +8 -0
  33. data/app/helpers/loyal_passport/users/omniauth_callbacks_helper.rb +8 -0
  34. data/app/helpers/loyal_passport/users/passwords_helper.rb +8 -0
  35. data/app/helpers/loyal_passport/users/registrations_helper.rb +8 -0
  36. data/app/helpers/loyal_passport/users/sessions_helper.rb +8 -0
  37. data/app/helpers/loyal_passport/users/unlocks_helper.rb +8 -0
  38. data/app/mailers/loyal_passport/users/mailer.rb +6 -0
  39. data/app/models/ability.rb +47 -0
  40. data/app/models/concerns/loyal_passport/homeworks_able.rb +36 -0
  41. data/app/models/loyal_passport/ability.rb +20 -0
  42. data/app/models/loyal_passport/assignment.rb +10 -0
  43. data/app/models/loyal_passport/homework.rb +42 -0
  44. data/app/models/loyal_passport/locker.rb +16 -0
  45. data/app/models/loyal_passport/oauth_bind.rb +6 -0
  46. data/app/models/loyal_passport/oauth_info.rb +36 -0
  47. data/app/models/loyal_passport/oauth_login.rb +9 -0
  48. data/app/models/loyal_passport/role.rb +25 -0
  49. data/app/models/session.rb +10 -0
  50. data/app/models/user.rb +99 -0
  51. data/app/views/layouts/loyal_passport/application.html.erb +42 -0
  52. data/app/views/loyal_passport/admin/loyal_passport/assignments/_assignment.html.erb +15 -0
  53. data/app/views/loyal_passport/admin/loyal_passport/roles/_form.html.erb +5 -0
  54. data/app/views/loyal_passport/admin/loyal_passport/roles/edit.html.erb +18 -0
  55. data/app/views/loyal_passport/admin/loyal_passport/roles/index.html.erb +25 -0
  56. data/app/views/loyal_passport/admin/loyal_passport/roles/new.html.erb +15 -0
  57. data/app/views/loyal_passport/admin/loyal_passport/roles/show.html.erb +59 -0
  58. data/app/views/loyal_passport/admin/users/_form.html.erb +27 -0
  59. data/app/views/loyal_passport/admin/users/edit.html.erb +14 -0
  60. data/app/views/loyal_passport/admin/users/index.html.erb +25 -0
  61. data/app/views/loyal_passport/admin/users/show.html.erb +27 -0
  62. data/app/views/loyal_passport/error/access_deny.html.erb +3 -0
  63. data/app/views/loyal_passport/users/confirmations/new.html.erb +19 -0
  64. data/app/views/loyal_passport/users/mailer/confirmation_instructions.html.erb +5 -0
  65. data/app/views/loyal_passport/users/mailer/reset_password_instructions.html.erb +8 -0
  66. data/app/views/loyal_passport/users/mailer/unlock_instructions.html.erb +7 -0
  67. data/app/views/loyal_passport/users/passwords/edit.html.erb +23 -0
  68. data/app/views/loyal_passport/users/passwords/new.html.erb +19 -0
  69. data/app/views/loyal_passport/users/profile/informations/index.html.erb +35 -0
  70. data/app/views/loyal_passport/users/registrations/edit.html.erb +47 -0
  71. data/app/views/loyal_passport/users/registrations/new.html.erb +40 -0
  72. data/app/views/loyal_passport/users/sessions/new.html.erb +26 -0
  73. data/app/views/loyal_passport/users/shared/_flashes.html.erb +8 -0
  74. data/app/views/loyal_passport/users/shared/_footer.html.erb +6 -0
  75. data/app/views/loyal_passport/users/shared/_links.erb +41 -0
  76. data/app/views/loyal_passport/users/shared/_nav.html.erb +1 -0
  77. data/app/views/loyal_passport/users/shared/_status.html.erb +18 -0
  78. data/app/views/loyal_passport/users/unlocks/new.html.erb +20 -0
  79. data/config/initializers/loyal_passport.rb +265 -0
  80. data/config/locales/loyal_passport/en/devise.en.yml +63 -0
  81. data/config/locales/loyal_passport/en/loyal_passport.en.yml +20 -0
  82. data/config/locales/loyal_passport/en/views/mailer.en.yml +15 -0
  83. data/config/locales/loyal_passport/zh-CN/devise.zh-CN.yml +65 -0
  84. data/config/locales/loyal_passport/zh-CN/loyal_passport.zh-CN.yml +47 -0
  85. data/config/locales/loyal_passport/zh-CN/models/loyal_passport/assignment.zh-CN.yml +24 -0
  86. data/config/locales/loyal_passport/zh-CN/user.yml +82 -0
  87. data/config/locales/loyal_passport/zh-CN/views/mailer.zh-CN.yml +14 -0
  88. data/config/routes.rb +36 -0
  89. data/db/migrate/20121123023890_create_users.rb +80 -0
  90. data/db/migrate/20121123025990_create_loyal_passport_roles.rb +24 -0
  91. data/db/migrate/20121123026990_create_loyal_passport_assignments.rb +24 -0
  92. data/db/migrate/20121125104021_add_sessions_table.rb +13 -0
  93. data/db/migrate/20130323050903_create_loyal_passport_lockers.rb +19 -0
  94. data/db/migrate/20130526062222_create_loyal_passport_homeworks.rb +12 -0
  95. data/db/migrate/20130625053301_create_loyal_passport_oauth_infos.rb +40 -0
  96. data/db/migrate/20130625053334_create_loyal_passport_oauth_binds.rb +17 -0
  97. data/db/migrate/20130625053410_create_loyal_passport_oauth_logins.rb +15 -0
  98. data/db/migrate/20130625055117_add_gender_to_users.rb +7 -0
  99. data/lib/loyal_passport/acts/acts_as_author_able.rb +26 -0
  100. data/lib/loyal_passport/acts/acts_as_locker_able.rb +26 -0
  101. data/lib/loyal_passport/acts.rb +12 -0
  102. data/lib/loyal_passport/config.rb +158 -0
  103. data/lib/loyal_passport/controllers/controller_extends.rb +28 -0
  104. data/lib/loyal_passport/controllers/custom_failure_app.rb +238 -0
  105. data/lib/loyal_passport/controllers/devise_extends.rb +26 -0
  106. data/lib/loyal_passport/controllers/passport_basic.rb +65 -0
  107. data/lib/loyal_passport/controllers/users_basic.rb +33 -0
  108. data/lib/loyal_passport/devise_helper.rb +22 -0
  109. data/lib/loyal_passport/engine.rb +11 -0
  110. data/lib/loyal_passport/user_parameter_sanitizer.rb +25 -0
  111. data/lib/loyal_passport/utils/array_util.rb +16 -0
  112. data/lib/loyal_passport/utils/data_util.rb +181 -0
  113. data/lib/loyal_passport/utils.rb +5 -0
  114. data/lib/loyal_passport/version.rb +4 -0
  115. data/lib/loyal_passport.rb +30 -0
  116. data/lib/tasks/loyal_passport_tasks.rake +4 -0
  117. metadata +209 -0
@@ -0,0 +1,238 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport::Controllers
3
+ class CustomFailureApp < ::Devise::FailureApp
4
+
5
+ def respond
6
+ if http_auth?
7
+ http_auth
8
+ elsif warden_options[:recall]
9
+ recall
10
+ else
11
+ redirect
12
+ end
13
+ end
14
+
15
+ def http_auth
16
+ self.status = 401
17
+ self.headers["WWW-Authenticate"] = %(Basic realm=#{Devise.http_authentication_realm.inspect}) if http_auth_header?
18
+ self.content_type = request.format.to_s
19
+ self.response_body = http_auth_body
20
+ end
21
+
22
+ def self.call(env)
23
+ @respond ||= action(:respond)
24
+ @respond.(env)
25
+ end
26
+
27
+ def recall
28
+ env["PATH_INFO"] = attempted_url
29
+ flash.now[:alert] = i18n_message(:invalid)
30
+ self.response = recall_app(warden_options[:recall]).call(env)
31
+ end
32
+
33
+ def redirect
34
+ # store_location!
35
+ options = {}
36
+
37
+ _redirect_url = redirect_url
38
+
39
+ if (return_to = failures_url_return_to) && (return_to != _redirect_url)
40
+ options[:return_to] = return_to
41
+ end
42
+
43
+ if flash[:timedout] && flash[:alert]
44
+ flash.keep(:timedout)
45
+ flash.keep(:alert)
46
+ else
47
+ flash[:alert] = i18n_message
48
+ end
49
+
50
+ if options.any?
51
+ redirect_to "#{_redirect_url}?#{options.to_query}"
52
+ else
53
+ redirect_to _redirect_url
54
+ end
55
+ end
56
+
57
+ protected
58
+
59
+ def i18n_message(default = nil)
60
+ message = warden_message || default || :unauthenticated
61
+
62
+ if message.is_a?(Symbol)
63
+ I18n.t(:"#{scope}.#{message}", :resource_name => scope,
64
+ :scope => "devise.failure", :default => [message])
65
+ else
66
+ message.to_s
67
+ end
68
+ end
69
+
70
+ #### 登录失败后要跳转的页面 ##################
71
+ def redirect_url
72
+ if warden_message == :timeout
73
+ flash[:timedout] = true
74
+ attempted_url || scope_url
75
+ else
76
+ scope_url
77
+ end
78
+ end
79
+
80
+ ### 这里写得倒是很妙 #################
81
+ def context_router
82
+ send(Devise.available_router_name)
83
+ end
84
+
85
+ def scope_url options={}
86
+ opts = {
87
+ # :host => ::LoyalPassport.config.host
88
+ :subdomain => ::LoyalPassport.config.subdomain
89
+ }.merge(
90
+ options
91
+ )
92
+
93
+ route = :"new_#{scope}_session_url"
94
+ opts[:format] = request_format unless skip_format?
95
+
96
+ config = Rails.application.config
97
+ opts[:script_name] = (config.relative_url_root if config.respond_to?(:relative_url_root))
98
+
99
+ context = context_router
100
+
101
+ if context.respond_to?(route)
102
+ context.send(route, opts)
103
+ elsif respond_to?(:root_url)
104
+ root_url(opts)
105
+ else
106
+ "/"
107
+ end
108
+ end
109
+
110
+ # def scope_path
111
+ # opts = {}
112
+ # route = :"new_#{scope}_session_path"
113
+ # opts[:format] = request_format unless skip_format?
114
+ #
115
+ # opts[:script_name] = (config.relative_url_root if config.respond_to?(:relative_url_root))
116
+ #
117
+ # context = send(Devise.available_router_name)
118
+ #
119
+ # if context.respond_to?(route)
120
+ # context.send(route, opts)
121
+ # elsif respond_to?(:root_path)
122
+ # root_path(opts)
123
+ # else
124
+ # "/"
125
+ # end
126
+ # end
127
+
128
+ def skip_format?
129
+ %w(html */*).include? request_format.to_s
130
+ end
131
+
132
+ # Choose whether we should respond in a http authentication fashion,
133
+ # including 401 and optional headers.
134
+ #
135
+ # This method allows the user to explicitly disable http authentication
136
+ # on ajax requests in case they want to redirect on failures instead of
137
+ # handling the errors on their own. This is useful in case your ajax API
138
+ # is the same as your public API and uses a format like JSON (so you
139
+ # cannot mark JSON as a navigational format).
140
+ def http_auth?
141
+ if request.xhr?
142
+ Devise.http_authenticatable_on_xhr
143
+ else
144
+ !(request_format && is_navigational_format?)
145
+ end
146
+ end
147
+
148
+ # It does not make sense to send authenticate headers in ajax requests
149
+ # or if the user disabled them.
150
+ def http_auth_header?
151
+ Devise.mappings[scope].to.http_authenticatable && !request.xhr?
152
+ end
153
+
154
+ def http_auth_body
155
+ return i18n_message unless request_format
156
+ method = "to_#{request_format}"
157
+
158
+ if method == "to_xml"
159
+ {
160
+ :error => i18n_message,
161
+ :code => :unauthorized,
162
+ :status => self.status,
163
+ :return_to => request.params[:return_to].to_s
164
+ }.to_xml(:root => "response")
165
+ elsif {}.respond_to?(method)
166
+ {
167
+ :response => {
168
+ :error => i18n_message,
169
+ :code => :unauthorized,
170
+ :status => self.status,
171
+ :return_to => request.params[:return_to].to_s
172
+ }
173
+ }.send(method)
174
+ else
175
+ i18n_message
176
+ end
177
+ end
178
+
179
+ def recall_app(app)
180
+ controller, action = app.split("#")
181
+ controller_name = ActiveSupport::Inflector.camelize(controller)
182
+ controller_klass = ActiveSupport::Inflector.constantize("#{controller_name}Controller")
183
+ controller_klass.action(action)
184
+ end
185
+
186
+ def warden
187
+ env['warden']
188
+ end
189
+
190
+ ####### Warden::Manager.before_failure config ######################
191
+ def warden_options
192
+ (env['warden.options'] ||= {})
193
+ end
194
+
195
+ def warden_message
196
+ @message ||= warden.message || warden_options[:message]
197
+ end
198
+
199
+ def scope
200
+ @scope ||= warden_options[:scope] || Devise.default_scope
201
+ end
202
+
203
+ def attempted_url
204
+ warden_options[:attempted_url]
205
+ end
206
+
207
+ def attempted_path
208
+ warden_options[:attempted_path]
209
+ end
210
+
211
+ # Stores requested uri to redirect the user after signing in. We cannot use
212
+ # scoped session provided by warden here, since the user is not authenticated
213
+ # yet, but we still need to store the uri based on scope, so different scopes
214
+ # would never use the same uri to redirect.
215
+ def store_location!
216
+ session["#{scope}_#{:return_to}"] = attempted_url if request.get? && !http_auth?
217
+ end
218
+
219
+ def failures_url_return_to
220
+ # get 请求 and 不是 http_auth
221
+ if request.get? && !http_auth?
222
+ attempted_url
223
+ else request.params[:return_to].present?
224
+ request.params[:return_to]
225
+ end
226
+ end
227
+
228
+ def is_navigational_format?
229
+ Devise.navigational_formats.include?(request_format)
230
+ end
231
+
232
+ def request_format
233
+ @request_format ||= request.format.try(:ref)
234
+ end
235
+
236
+ end
237
+ end
238
+
@@ -0,0 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport::Controllers
3
+ ### devise 扩展 #####################################
4
+ module DeviseExtends
5
+ # 登录完成后跳转的页面
6
+ def after_sign_in_path_for(resource)
7
+ if params[:return_to].present?
8
+ params[:return_to]
9
+ else
10
+ loyal_passport_app.users_profile_root_url
11
+ end
12
+
13
+ end
14
+
15
+ #### 更新完
16
+ def after_update_path_for(resource)
17
+ if params[:return_to].present?
18
+ params[:return_to]
19
+ else
20
+ loyal_passport_app.users_profile_root_url
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,65 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport::Controllers
3
+ # 通行证相关的逻辑,用于用户验证,登录,身份以及权限等
4
+ module PassportBasic
5
+ def self.included(base)
6
+ base.class_eval do
7
+ extend ClassMethods
8
+
9
+ include SharedMethods
10
+
11
+ helper SharedHelperMethods
12
+
13
+ include InstanceMethods
14
+
15
+ rescue_from ::CanCan::AccessDenied do |exception|
16
+ # :redirect_to, 'http://www.ruby800.com', :alert => 'Access Denied'
17
+ # :render, :text => 'Access Denied'
18
+ # redirect_to root_url, :alert => exception.message
19
+
20
+ call_params = ::LoyalPassport.config.resuce_cancan_access_denied_call
21
+ case call_params[0]
22
+ when :redirect_to
23
+ redirect_to call_params[1] || "/", call_params[2] || {}
24
+ else #### render and else
25
+ render call_params[1], call_params[2] || {}
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ module ClassMethods
32
+
33
+ end
34
+
35
+ module SharedMethods
36
+ # FIXME: 需要更好的写法
37
+ def loyal_authenticate_admin!
38
+ authenticate_user!
39
+ authorize! :manage, :all
40
+ end
41
+
42
+ # 当前用户的ability
43
+ def current_user_ability
44
+ @current_user_ability ||= (current_user || User.new).ability
45
+ end
46
+
47
+ end
48
+
49
+ module SharedHelperMethods
50
+ def output_user_image_tag user, options={}
51
+ image_tag(user.avatar.url(options[:style] || :tiny))
52
+ end
53
+
54
+ # 渲染返回的隐藏域
55
+ def util_output_return_to_hidden_field_tag value=request.url
56
+ hidden_field_tag :return_to, params[:return_to] || value, :id => nil
57
+ end
58
+ end
59
+
60
+ module InstanceMethods
61
+
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,33 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport::Controllers
3
+ module UsersBasic
4
+ def self.included(base)
5
+ base.class_eval do
6
+ include InstanceMethods
7
+ include ::LoyalPassport::Controllers::DeviseExtends
8
+
9
+ before_filter :init_loyal_passport_request
10
+
11
+ layout 'loyal_passport/application'
12
+
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+ def init_loyal_passport_request
18
+
19
+ end
20
+
21
+ protected
22
+
23
+ def devise_parameter_sanitizer
24
+ if resource_class == ::User
25
+ ::LoyalPassport::User::ParameterSanitizer.new(::User, :user, params)
26
+ else
27
+ super # Use the default one
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport
3
+ module DeviseHelper
4
+ def devise_error_messages!
5
+ return "" if resource.errors.empty?
6
+ messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
7
+ sentence = I18n.t(
8
+ "loyal_passport.form.submit",
9
+ :count => resource.errors.count
10
+ )
11
+
12
+ html = <<-HTML
13
+ <div class="error-explanation">
14
+ <h2>#{sentence}</h2>
15
+ <ul>#{messages}</ul>
16
+ </div>
17
+ HTML
18
+
19
+ html.html_safe
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace LoyalPassport
5
+
6
+ config.generators do |g|
7
+ g.test_framework :rspec, :view_specs => true
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class ::LoyalPassport::User::ParameterSanitizer < Devise::ParameterSanitizer
3
+ def sign_in
4
+ default_params.permit(*(
5
+ auth_keys + [
6
+ :password,
7
+ :remember_me,
8
+ ]
9
+ ))
10
+ end
11
+
12
+ def sign_up
13
+ default_params.permit(*(auth_keys + [
14
+ :nick_name, :permalink, :true_name, :captcha, :captcha_key,
15
+ :password, :password_confirmation, :avatar, :avatar_cache
16
+ ]))
17
+ end
18
+
19
+ def account_update
20
+ default_params.permit(*(auth_keys + [
21
+ :password, :password_confirmation, :current_password, :avatar, :avatar_cache
22
+ ]))
23
+ end
24
+
25
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport
3
+ class ArrayUtil
4
+ def self.extract_options!(arr)
5
+ if arr.last.is_a?(Hash)
6
+ arr.pop
7
+ else
8
+ {}
9
+ end
10
+ end
11
+
12
+ def self.init args
13
+ args.is_a?(Array) ? args : (args.nil? ? [] : [args])
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,181 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport
3
+ class DataUtil < ::Hash
4
+ def initialize(options={})
5
+ self.merge! options
6
+ end
7
+
8
+ def []=(name, value)
9
+ super(name.to_sym, value)
10
+ end
11
+
12
+ def to_ary
13
+ end
14
+
15
+ def [](name)
16
+ super(name.to_sym)
17
+ end
18
+
19
+ def deep_merge(other_hash)
20
+ self.deep_dup.deep_merge!(other_hash)
21
+ end
22
+
23
+ # 深层的合并
24
+ def deep_merge!(other_hash)
25
+ other_hash = self.class.new(other_hash) unless other_hash.is_a?(self.class)
26
+
27
+ other_hash.each_pair do |k,v|
28
+ tv = self[k]
29
+
30
+ unless v.is_a?(self.class)
31
+ v = self.class.new(v) if v.is_a?(Hash)
32
+ end
33
+
34
+ self[k] = tv.is_a?(self.class) && v.is_a?(self.class) ? tv.deep_merge(v) : v
35
+ end
36
+
37
+ self
38
+ end
39
+
40
+ def deep_dup
41
+ impl_deep_dup self
42
+ end
43
+
44
+ def method_missing method_name, *args, &block
45
+ # 如果方法是赋值, 则直接赋值
46
+ if method_name.to_s.end_with?('=')
47
+ self[method_name.to_s.chop!] = args.first
48
+ else
49
+ result = self[method_name.to_sym]
50
+
51
+ if result.is_a?(Hash) && !(result.is_a?(self.class))
52
+ self[method_name] = self.class.new(result)
53
+ elsif result.nil?
54
+ self[method_name] = self.class.new
55
+ else
56
+ result
57
+ end
58
+ end
59
+ end
60
+
61
+ def stringify_keys!
62
+ impl_stringify_keys! self
63
+ end
64
+
65
+ def symbolize_keys!
66
+ impl_symbolize_keys! self
67
+ end
68
+
69
+ #
70
+ # origin_hash_symbolize
71
+ # origin_hash_stringify
72
+ # origin_hash_symbolize! key全是 symbol的
73
+ # origin_hash_stringify! key全市 string 的
74
+ #
75
+ [:symbolize, :stringify].each do |method_name|
76
+ define_method "origin_hash_#{method_name}".to_sym do
77
+ self.deep_dup.send "origin_hash_#{method_name}!".to_sym
78
+ end
79
+
80
+ define_method "origin_hash_#{method_name}!".to_sym do
81
+ self.send "impl_origin_hash_#{method_name}".to_sym, self
82
+ end
83
+ end
84
+
85
+ #
86
+ # deep_stringify_keys
87
+ # deep_stringify_keys!
88
+ #
89
+ [:stringify, :symbolize].each do |method_name|
90
+ method_name_impl = "impl_#{method_name}_keys".to_sym
91
+ method_name_new_soft = "deep_#{method_name}_keys".to_sym
92
+ method_name_new = "deep_#{method_name}_keys!".to_sym
93
+
94
+ define_method method_name_new_soft do
95
+ self.deep_dup.send method_name_new
96
+ end
97
+
98
+ define_method method_name_new do
99
+ tmp = self.send method_name_impl, self
100
+
101
+ tmp.keys.each do |key|
102
+ tmp[key] = self.send method_name_impl, tmp[key]
103
+ end
104
+
105
+ tmp
106
+ end
107
+ end
108
+
109
+ private
110
+
111
+ def impl_deep_dup v
112
+ if v.is_a?(::Hash)
113
+ tv = v.dup
114
+
115
+ v.each do |key, value|
116
+ tv[key] = impl_deep_dup value
117
+ end
118
+
119
+ tv
120
+ elsif v.is_a?(::Array)
121
+ v.map do |_v|
122
+ impl_deep_dup _v
123
+ end
124
+ else
125
+ v
126
+ end
127
+ end
128
+
129
+ [:stringify, :symbolize].each do |method_name|
130
+ typify_method = {:symbolize => :to_sym, :stringify => :to_s}[method_name]
131
+ method_name_new = "impl_origin_hash_#{method_name}".to_sym
132
+ define_method method_name_new do |v|
133
+ if v.is_a?(::Hash)
134
+ tv = Hash.new
135
+
136
+ v.keys.each do |key|
137
+ tv[key.send(typify_method)] = self.send(method_name_new, v[key])
138
+ end
139
+
140
+ tv
141
+ elsif v.is_a?(::Array)
142
+ v.map do |_v|
143
+ self.send(method_name_new, _v)
144
+ end
145
+ else
146
+ v
147
+ end
148
+ end
149
+ end
150
+
151
+ [:stringify, :symbolize].each do |method_name|
152
+ typify_clazz = {:stringify => ::String, :symbolize => ::Hash}[method_name]
153
+ method_name_new = "impl_#{method_name}_keys".to_sym
154
+ typify_method = {:symbolize => :to_sym, :stringify => :to_s}[method_name]
155
+
156
+ define_method method_name_new do |v|
157
+ if v.is_a?(Hash)
158
+ v.keys.each do |key|
159
+ kv = v.delete key
160
+
161
+ unless key.is_a?(typify_clazz)
162
+ if(kv.is_a?(::Hash) && !(kv.is_a?(self.class)))
163
+ kv = self.class.new(kv)
164
+ end
165
+ end
166
+
167
+ v[key.send(typify_method)] = self.send method_name_new, kv
168
+ end
169
+ elsif v.is_a?(Array)
170
+ v = v.map do |_v|
171
+ self.send method_name_new, _v
172
+ end
173
+ end
174
+
175
+ v
176
+ end
177
+ end
178
+
179
+ end
180
+ end
181
+
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport
3
+ autoload :DataUtil, 'loyal_passport/utils/data_util'
4
+ autoload :ArrayUtil, 'loyal_passport/utils/array_util'
5
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalPassport
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'loyal_passport/utils'
3
+ require "loyal_passport/engine"
4
+ require "loyal_passport/config"
5
+ require "loyal_passport/acts"
6
+
7
+ require "loyal_passport/controllers/controller_extends"
8
+
9
+ module LoyalPassport
10
+ module Controllers
11
+ autoload :DeviseExtends, 'loyal_passport/controllers/devise_extends'
12
+ autoload :PassportBasic, 'loyal_passport/controllers/passport_basic'
13
+ autoload :CustomFailureApp, 'loyal_passport/controllers/custom_failure_app'
14
+ autoload :UsersBasic, 'loyal_passport/controllers/users_basic'
15
+ end
16
+
17
+ autoload :DeviseHelper, 'loyal_passport/devise_helper'
18
+ autoload :UtilHelper, 'loyal_passport/util_helper'
19
+
20
+ module User
21
+ autoload :ParameterSanitizer, 'loyal_passport/user_parameter_sanitizer'
22
+ end
23
+ end
24
+
25
+ I18n.load_path += Dir[Pathname.new(__FILE__).join('..', '..', 'config', 'locales', '**', '*.{rb,yml}').to_s]
26
+
27
+ if defined?(::ActionController::Base)
28
+ ::ActionController::Base.send :include, ::LoyalPassport::Controllers::PassportBasic
29
+ end
30
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :loyal_passport do
3
+ # # Task goes here
4
+ # end