user_naming 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33ea227853001c9d8674cfd5703ebcaad565b2dd
4
+ data.tar.gz: a2a2eb43f59a8ef1daa9433fa7fc9b99af37450e
5
+ SHA512:
6
+ metadata.gz: b9c30d24ecd52fd29db5309ec3ce320f0894704214a54f7598ee1d517c5dbf686e6ab196906a1b4873c83b0c432c61584be546fa6d3d3394a123a75f3f578edc
7
+ data.tar.gz: 648a0e057fcb4b16b570c9a114d15dd5ff14c109c1a372582740db543a3ac14ccf7713c5a50ae1ae4b124e4cbccca775e534167ef6ffa150c803f6ff6e52374c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ cache: bundler
2
+
3
+ language:
4
+ -ruby
5
+
6
+ rvm:
7
+ - 2.3.3
8
+
9
+ before_install: gem install bundler -v 1.14.6
10
+
11
+ sudo: false
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tomichj@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in user_naming.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Justin Tomich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # UserNaming
2
+
3
+ UserNaming is a simple gem that provides a 'name' column and some methods to standardize displaying user names in
4
+ rails applications.
5
+
6
+ This isn't a big gem: it's a migration and a module with 4 or 5 methods that you include in your 'user' class.
7
+ But, it lets me deal with user's names in the same way in every application I build. Over time, this pays off.
8
+
9
+ This gem currently supports rails 5 and beyond; adding 4.2 support is trivial, but would also require
10
+ adding an `appraisal` configuration. Open for PRs if interested.
11
+
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's `Gemfile` and run `bundle install` to install it:
16
+
17
+ ```ruby
18
+ gem 'user_naming'
19
+ ```
20
+
21
+ Then run the UserNaming install generator:
22
+
23
+ ```sh
24
+ rails generate user_naming:install
25
+ ```
26
+
27
+ If your user model is not `User`, you can (optionally) specify a user model with the `--model` flag. For example,
28
+ if your user model class is `Person`:
29
+
30
+ ```sh
31
+ rails generate user_naming:install --model Person
32
+ ```
33
+
34
+ After you've run the install generator, apply the migration that was just generated:
35
+ ```sh
36
+ rails db:migrate
37
+ ```
38
+
39
+ The generator does the following:
40
+
41
+ * Insert `include UserNaming::User` into your `User` model.
42
+ * Create a migration to add a `name` column to your `User` table.
43
+
44
+
45
+ ## Usage
46
+
47
+ UserNaming supports setting the user's name as one field via `name=`.
48
+
49
+ Non-mutating accessors to read the user's name in different formats are provided:
50
+
51
+ - name - the full name
52
+ - first_name - always the first word of the name
53
+ - middle name - everything that's not the first or last name
54
+ - last_name - the last word of the name
55
+ - initials - initials, joined without period or any other delimiter
56
+ - first_name_last_initial - first name, last initial with a .
57
+
58
+ That's it!
59
+
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tomichj/user_naming.
64
+
65
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to
66
+ adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
67
+
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
72
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "user_naming"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,102 @@
1
+ module UserNaming
2
+ module Generators
3
+ module Helpers
4
+ private
5
+
6
+ def copy_migration(migration_name, config = {})
7
+ unless migration_exists?(migration_name)
8
+ migration_template(
9
+ "db/migrate/#{migration_name}",
10
+ "db/migrate/#{migration_name}",
11
+ config.merge(migration_version: migration_version)
12
+ )
13
+ end
14
+ end
15
+
16
+
17
+ def migration_exists?(name)
18
+ existing_migrations.include?(name)
19
+ end
20
+
21
+ def existing_migrations
22
+ @existing_migrations ||= Dir.glob('db/migrate/*.rb').map do |file|
23
+ migration_name_without_timestamp(file)
24
+ end
25
+ end
26
+
27
+ def migration_name_without_timestamp(file)
28
+ file.sub(%r{^.*(db/migrate/)(?:\d+_)?}, '')
29
+ end
30
+
31
+
32
+
33
+
34
+ def migration_version
35
+ if Rails.version >= '5.0.0'
36
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
37
+ end
38
+ end
39
+
40
+
41
+
42
+ # Either return the model passed in a classified form or return the default "User".
43
+ def model_class_name
44
+ options[:model] ? options[:model].classify : 'User'
45
+ end
46
+
47
+ def model_path
48
+ @model_path ||= File.join('app', 'models', "#{file_path}.rb")
49
+ end
50
+
51
+ def file_path
52
+ model_name.underscore
53
+ end
54
+
55
+ def namespace
56
+ Rails::Generators.namespace if Rails::Generators.respond_to?(:namespace)
57
+ end
58
+
59
+ def namespaced?
60
+ !namespace.nil?
61
+ end
62
+
63
+ def model_name
64
+ if namespaced?
65
+ [namespace.to_s] + [model_class_name]
66
+ else
67
+ [model_class_name]
68
+ end.join('::')
69
+ end
70
+
71
+ def table_reference_name
72
+ table_name.singularize
73
+ end
74
+
75
+ def table_name
76
+ @table_name ||= begin
77
+ base = plural_name
78
+ (class_path + [base]).join('_')
79
+ end
80
+ end
81
+
82
+ def class_path
83
+ @class_path
84
+ end
85
+
86
+ def singular_name
87
+ @file_name
88
+ end
89
+
90
+ def plural_name
91
+ singular_name.pluralize
92
+ end
93
+
94
+ def assign_names!(name) #:nodoc:
95
+ @class_path = name.include?('/') ? name.split('/') : name.split('::')
96
+ @class_path.map!(&:underscore)
97
+ @file_name = @class_path.pop
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,48 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/active_record'
3
+ require 'generators/user_naming/helpers'
4
+
5
+ module UserNaming
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ include Rails::Generators::Migration
9
+ include UserNaming::Generators::Helpers
10
+
11
+ source_root File.expand_path('../templates', __FILE__)
12
+ class_option :model,
13
+ optional: true,
14
+ type: :string,
15
+ banner: 'model',
16
+ desc: "Specify the model class name if you will use anything other than 'User'"
17
+
18
+ def initialize(*)
19
+ super
20
+ assign_names!(model_class_name)
21
+ end
22
+
23
+ def verify
24
+ # if options[:model] && !File.exist?(model_path)
25
+ if !File.exist?(model_path)
26
+ puts "Exiting: the model class #{options[:model] || 'User' } is not found. You must have a valid user model."
27
+ exit 1
28
+ end
29
+ end
30
+
31
+ def inject_into_user_model
32
+ inject_into_class(model_path, model_class_name, " include UserNaming::User\n")
33
+ end
34
+
35
+ def create_name_migration
36
+ copy_migration 'add_name_to_users.rb'
37
+ end
38
+
39
+ private
40
+
41
+ # for generating a timestamp when using `create_migration`
42
+ def self.next_migration_number(dir)
43
+ ActiveRecord::Generators::Base.next_migration_number(dir)
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ class AddNameToUsers < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ add_column :users, :name, :string
4
+ end
5
+ end
@@ -0,0 +1,76 @@
1
+ module UserNaming
2
+ module User
3
+ extend ActiveSupport::Concern
4
+
5
+ # First name.
6
+ #
7
+ # The first name is always the first word of the name.
8
+ #
9
+ # @return [String]
10
+ def first_name
11
+ name.split.first
12
+ end
13
+
14
+
15
+ # Middle name.
16
+ #
17
+ # The middle name is everything between the first name and last name,
18
+ # or empty string.
19
+ #
20
+ # @return [String]
21
+ def middle_name
22
+ name.split[1..-2].join(' ')
23
+ end
24
+
25
+ # The last name.
26
+ #
27
+ # A name with only one part will return an empty string.
28
+ # A name with two or more parts will return everything after the first part.
29
+ #
30
+ # Examples:
31
+ # 'Bilbo' will have a last name of ''.
32
+ # 'Bilbo Foo Baggins' will have a last name of 'Foo Baggins'.
33
+ #
34
+ # @return [String]
35
+ def last_name
36
+ if name.split.count > 1
37
+ name.split[-1]
38
+ else
39
+ ''
40
+ end
41
+ end
42
+
43
+ # Initials.
44
+ #
45
+ # The initials are the first letter of each name part,
46
+ # joined together *without* periods.
47
+ #
48
+ # Examples:
49
+ # 'Bilbo' will have initials of 'B'.
50
+ # 'Bilbo Foo Baggins' will have initials of 'BFB'.
51
+ # 'Bilbo Bartlet Foo Baggins' will have initials of 'BBFB'.
52
+ #
53
+ # @return [String]
54
+ def initials
55
+ name.split.collect{|p| p[0].upcase }.join
56
+ end
57
+
58
+ # The first name and the last initial.
59
+ #
60
+ # If only one name, just the first name is returned.
61
+ #
62
+ # Examples:
63
+ # 'Bilbo' will be 'B'
64
+ # 'Bilbo Foo Baggins' will be 'Bilbo B'
65
+ #
66
+ # @return [String]
67
+ def first_name_last_initial
68
+ if name.split.count > 1
69
+ first_name + ' ' + last_name[0].upcase + '.'
70
+ else
71
+ first_name
72
+ end
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module UserNaming
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_support'
2
+ require 'user_naming/version'
3
+ require 'user_naming/user'
4
+
5
+ module UserNaming
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,30 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'user_naming/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'user_naming'
7
+ spec.version = UserNaming::VERSION
8
+ spec.authors = ['Justin Tomich']
9
+ spec.email = ['justin@tomich.org']
10
+ spec.homepage = 'http://github.com/tomichj/user_naming'
11
+ spec.summary = 'A rails gem providing naming methods for user models'
12
+ spec.description = ''
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.test_files = `git ls-files -- {spec}/*`.split("\n")
19
+
20
+ spec.require_paths = ['lib']
21
+ spec.extra_rdoc_files = %w[LICENSE.txt README.md CODE_OF_CONDUCT.md]
22
+ spec.rdoc_options = ['--charset=UTF-8']
23
+
24
+ spec.add_dependency 'rails', '>= 5.0', '< 5.2'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'factory_girl'
30
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: user_naming
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Tomich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: factory_girl
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: ''
90
+ email:
91
+ - justin@tomich.org
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files:
95
+ - LICENSE.txt
96
+ - README.md
97
+ - CODE_OF_CONDUCT.md
98
+ files:
99
+ - ".gitignore"
100
+ - ".rspec"
101
+ - ".travis.yml"
102
+ - CODE_OF_CONDUCT.md
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - bin/console
108
+ - bin/setup
109
+ - lib/generators/user_naming/helpers.rb
110
+ - lib/generators/user_naming/install/install_generator.rb
111
+ - lib/generators/user_naming/install/templates/db/migrate/add_name_to_users.rb
112
+ - lib/user_naming.rb
113
+ - lib/user_naming/user.rb
114
+ - lib/user_naming/version.rb
115
+ - user_naming.gemspec
116
+ homepage: http://github.com/tomichj/user_naming
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options:
122
+ - "--charset=UTF-8"
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.5.2
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: A rails gem providing naming methods for user models
141
+ test_files: []