username 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30a2016ad31e9aabae42451d8f93f7f090ac25659a77bc35b4a1933ea60e4159
4
- data.tar.gz: aafae93ad37efc1eb2ef0e7c887c47c1f37fcf29802676a772f4cff3e81b1cbf
3
+ metadata.gz: a76b0ea615f4def3a94b66ca1a024202c36bf106c9261ff20cfee0a95e051cfa
4
+ data.tar.gz: 809f5283ee7f6b15a56e99802afa6767c1513c378072afa032dc21d171706882
5
5
  SHA512:
6
- metadata.gz: c1ba518f119955a8eb4982f73824f667babb33b5df160ef88178104e2fb833c617b6ec74e89091fc805056cbb0bad7e3bc0ce67732c25a45c3e113910f929edc
7
- data.tar.gz: 83c2e53ab7d4f52d66f04f84f3b854b325131466be304ba70f5accd4b831a246710d30d88288d2aaec9d9ea77e3d5fdccce7e2652ee1147d75583dc61f886b99
6
+ metadata.gz: 8bebd2607b4a0b2bf24dfb973524799ca074bce7424113a1d98b1bb78667da6c149f440968801b7fd0fd273242b30330883f87bd483b140cc6669049df63220f
7
+ data.tar.gz: 15456eb519fbe2062ebb7cab85f063774f9f9e4e6e3535a5fd01cf45744a621aea1048e4c6fa2926e0bb49e5cbd4a0deba17ee949e6ab052c9a4e56c1e4e8348
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  * nothing yet
6
6
 
7
+ ### 1.1.0 - 2018/01/24
8
+
9
+ * features
10
+ * add Devise login implementation
11
+
7
12
  ### 1.0.1 - 2018/01/20
8
13
 
9
14
  * bugfixes
data/README.md CHANGED
@@ -11,6 +11,7 @@ Usernames for ActiveRecord model.
11
11
  * [Installation](#installation)
12
12
  * [Usage](#usage)
13
13
  * [Methods](#methods)
14
+ * [Devise](#devise)
14
15
  * [Configuration](#configuration)
15
16
  * [To Do](#to-do)
16
17
  * [Contributing](#contributing)
@@ -72,6 +73,42 @@ User.username_valid? 'test'
72
73
  User.first.username_available? 'test'
73
74
  ```
74
75
 
76
+ ### Devise
77
+
78
+ If you are using Devise and you want to allow your users to sign in using either their username or email address, here is how you'd do that:
79
+
80
+ 1) Pass the `devise` option to the `has_username` method:
81
+
82
+ ```ruby
83
+ class User < ApplicationRecord
84
+ has_username devise: true
85
+ end
86
+ ```
87
+
88
+ 2) Configure permitted Devise parameters in your `ApplicationController`:
89
+
90
+ ```ruby
91
+ class ApplicationController < ActionController::Base
92
+
93
+ before_action :configure_permitted_parameters, if: :devise_controller?
94
+
95
+ def configure_permitted_parameters
96
+ attributes = [:username, :email, :password, :remember_me]
97
+ devise_parameter_sanitizer.permit :sign_up, keys: attributes
98
+ devise_parameter_sanitizer.permit :account_update, keys: attributes
99
+ end
100
+
101
+ end
102
+ ```
103
+
104
+ 3) Replace `:email` in your sign in form with `:login`:
105
+
106
+ ```haml
107
+ = simple_form_for resource, as: resource_name, url: session_url(resource_name) do |f|
108
+ = f.input :login, required: true, autofocus: true
109
+ = f.input :password, required: true
110
+ ```
111
+
75
112
  ---
76
113
 
77
114
  ## Configuration
@@ -4,9 +4,19 @@ module Username
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  module ClassMethods
7
- def has_username
7
+ def has_username options = {}
8
+ defaults = {
9
+ devise: false
10
+ }
11
+ options = defaults.merge options
12
+
8
13
  Username.configuration&.models << self.name
9
14
  validate :username_validation
15
+
16
+ if options[:devise]
17
+ attr_accessor :login
18
+ extend Username::Model::DeviseMethods
19
+ end
10
20
  end
11
21
 
12
22
  def username_valid? username
@@ -27,6 +37,17 @@ module Username
27
37
  end
28
38
  end
29
39
 
40
+ module DeviseMethods
41
+ def find_for_database_authentication warden_conditions
42
+ conditions = warden_conditions.dup
43
+ if login = conditions.delete(:login)
44
+ where(conditions.to_h).where(['lower(username) = :value OR lower(email) = :value', { value: login.downcase }]).first
45
+ elsif conditions.has_key?(:username) || conditions.has_key?(:email)
46
+ where(conditions.to_h).first
47
+ end
48
+ end
49
+ end
50
+
30
51
  def username_available? username
31
52
  unless username.nil?
32
53
  valid = true
@@ -1,5 +1,5 @@
1
1
  module Username
2
2
 
3
- VERSION = '1.0.1'
3
+ VERSION = '1.1.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: username
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-20 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties