url_slug 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1e826d6198dc0c58d0feb8dd633c7dbc091d1136a7628e07482d6ead03b1ecc7
4
+ data.tar.gz: a35d57158513f68267ad17fbca91536ebb6f935a39d2c5ad2373d9d56ea61fcc
5
+ SHA512:
6
+ metadata.gz: 0dc24817c0121fbf61b64967c06039634b0cf6b8cde7dbd842a1b403786ea63b578b6fd8f6a6c1b3eed153e9e9660a772dd831395d31a6cad30ba2262173fb8c
7
+ data.tar.gz: db8c904a42d4230b102cc13cbd6fe271f95b8de94c3135f2481ec60af7bc0d3c362e87a1aecd05c093af9e6d15b301e9a0e3292127764fe34392362dba0e5b0c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.6
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+
12
+ Style/StringLiteralsInInterpolation:
13
+ Enabled: true
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ AllCops:
22
+ Exclude:
23
+ - url_slug.gemspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-30
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at goran@domain.tld. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in url_slug.gemspec
6
+ gemspec
7
+
8
+ gem 'activesupport'
9
+ gem 'thor'
10
+
11
+ group :development do
12
+ gem 'rake', '~> 13.0'
13
+ end
14
+
15
+ group :development, :test do
16
+ gem 'byebug'
17
+ gem 'rspec', '~> 3.0', require: false
18
+ gem 'rubocop', '~> 1.21', require: false
19
+ gem 'rubocop-performance', require: false
20
+ gem 'rubocop-rake', require: false
21
+ gem 'rubocop-rspec', require: false
22
+ end
23
+
24
+ group :test do
25
+ gem 'i18n', '~> 1.13', require: false
26
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,139 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ url_slug (0.1.1)
5
+ activesupport (>= 4.0.0)
6
+ thor
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (7.0.5)
12
+ actionview (= 7.0.5)
13
+ activesupport (= 7.0.5)
14
+ rack (~> 2.0, >= 2.2.4)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
+ actionview (7.0.5)
19
+ activesupport (= 7.0.5)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
+ activemodel (7.0.5)
25
+ activesupport (= 7.0.5)
26
+ activerecord (7.0.5)
27
+ activemodel (= 7.0.5)
28
+ activesupport (= 7.0.5)
29
+ activesupport (7.0.5)
30
+ concurrent-ruby (~> 1.0, >= 1.0.2)
31
+ i18n (>= 1.6, < 2)
32
+ minitest (>= 5.1)
33
+ tzinfo (~> 2.0)
34
+ ast (2.4.2)
35
+ builder (3.2.4)
36
+ byebug (11.1.3)
37
+ concurrent-ruby (1.2.2)
38
+ crass (1.0.6)
39
+ diff-lcs (1.5.0)
40
+ erubi (1.12.0)
41
+ i18n (1.13.0)
42
+ concurrent-ruby (~> 1.0)
43
+ json (2.6.3)
44
+ loofah (2.21.3)
45
+ crass (~> 1.0.2)
46
+ nokogiri (>= 1.12.0)
47
+ method_source (1.0.0)
48
+ minitest (5.18.0)
49
+ nokogiri (1.15.2-x86_64-linux)
50
+ racc (~> 1.4)
51
+ parallel (1.23.0)
52
+ parser (3.2.2.1)
53
+ ast (~> 2.4.1)
54
+ racc (1.6.2)
55
+ rack (2.2.7)
56
+ rack-test (2.1.0)
57
+ rack (>= 1.3)
58
+ rails-dom-testing (2.0.3)
59
+ activesupport (>= 4.2.0)
60
+ nokogiri (>= 1.6)
61
+ rails-html-sanitizer (1.6.0)
62
+ loofah (~> 2.21)
63
+ nokogiri (~> 1.14)
64
+ railties (7.0.5)
65
+ actionpack (= 7.0.5)
66
+ activesupport (= 7.0.5)
67
+ method_source
68
+ rake (>= 12.2)
69
+ thor (~> 1.0)
70
+ zeitwerk (~> 2.5)
71
+ rainbow (3.1.1)
72
+ rake (13.0.6)
73
+ regexp_parser (2.8.0)
74
+ rexml (3.2.5)
75
+ rspec (3.12.0)
76
+ rspec-core (~> 3.12.0)
77
+ rspec-expectations (~> 3.12.0)
78
+ rspec-mocks (~> 3.12.0)
79
+ rspec-core (3.12.2)
80
+ rspec-support (~> 3.12.0)
81
+ rspec-expectations (3.12.3)
82
+ diff-lcs (>= 1.2.0, < 2.0)
83
+ rspec-support (~> 3.12.0)
84
+ rspec-mocks (3.12.5)
85
+ diff-lcs (>= 1.2.0, < 2.0)
86
+ rspec-support (~> 3.12.0)
87
+ rspec-support (3.12.0)
88
+ rubocop (1.51.0)
89
+ json (~> 2.3)
90
+ parallel (~> 1.10)
91
+ parser (>= 3.2.0.0)
92
+ rainbow (>= 2.2.2, < 4.0)
93
+ regexp_parser (>= 1.8, < 3.0)
94
+ rexml (>= 3.2.5, < 4.0)
95
+ rubocop-ast (>= 1.28.0, < 2.0)
96
+ ruby-progressbar (~> 1.7)
97
+ unicode-display_width (>= 2.4.0, < 3.0)
98
+ rubocop-ast (1.28.1)
99
+ parser (>= 3.2.1.0)
100
+ rubocop-capybara (2.18.0)
101
+ rubocop (~> 1.41)
102
+ rubocop-factory_bot (2.23.1)
103
+ rubocop (~> 1.33)
104
+ rubocop-performance (1.18.0)
105
+ rubocop (>= 1.7.0, < 2.0)
106
+ rubocop-ast (>= 0.4.0)
107
+ rubocop-rake (0.6.0)
108
+ rubocop (~> 1.0)
109
+ rubocop-rspec (2.22.0)
110
+ rubocop (~> 1.33)
111
+ rubocop-capybara (~> 2.17)
112
+ rubocop-factory_bot (~> 2.22)
113
+ ruby-progressbar (1.13.0)
114
+ thor (1.2.2)
115
+ tzinfo (2.0.6)
116
+ concurrent-ruby (~> 1.0)
117
+ unicode-display_width (2.4.2)
118
+ zeitwerk (2.6.8)
119
+
120
+ PLATFORMS
121
+ x86_64-linux
122
+
123
+ DEPENDENCIES
124
+ activerecord (>= 4.0)
125
+ activesupport
126
+ byebug
127
+ i18n (~> 1.13)
128
+ railties (>= 4.0)
129
+ rake (~> 13.0)
130
+ rspec (~> 3.0)
131
+ rubocop (~> 1.21)
132
+ rubocop-performance
133
+ rubocop-rake
134
+ rubocop-rspec
135
+ thor
136
+ url_slug!
137
+
138
+ BUNDLED WITH
139
+ 2.3.13
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
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,133 @@
1
+ # UrlSlug
2
+
3
+ UrlSlug is a slug/permalink plugin with ActiveRecord and I18n support. It lets you create SEO friendly URLs.
4
+
5
+ With UrlSlug, it's easy to make your application use URLs like:
6
+
7
+ https://example.com/posts/this-is-a-friendly-url
8
+
9
+ or
10
+
11
+ https://example.com/posts/123-this-is-a-friendly-url
12
+
13
+ instead of:
14
+
15
+ https://example.com/posts/123
16
+
17
+ ## Usage
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'url_slug'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ ```shell
28
+ bundle install
29
+ ```
30
+
31
+ Run the generator
32
+
33
+ ```shell
34
+ # thor url_slug:install ClassName field:attr --path="path/to/class.rb"
35
+ thor url_slug:install Person name:slug --path="libs/person.rb"
36
+ ```
37
+
38
+ or add it manually
39
+
40
+
41
+ ```ruby
42
+ class Person
43
+ include UrlSlug
44
+ url_slug_for :name, attr: :slug
45
+
46
+ ...
47
+ end
48
+
49
+ person = Person.new
50
+ person.name = "John Doe"
51
+ person.slug #=> "john-doe"
52
+ ```
53
+
54
+ The `slug` attribute is optional. If you dont define it, then `:name_slug` will be used as attribute
55
+
56
+ ```
57
+ url_slug_for :name # creates :name_slug attribute
58
+
59
+ person.name_slug
60
+ ```
61
+
62
+ ## Ruby on Rails
63
+
64
+ Run the install generator.
65
+
66
+ ```shell
67
+ # rails g url_slug:rails ClassName field:field_slug --migration
68
+ rails g url_slug:rails Person name --migration
69
+ ```
70
+
71
+ or add it manually
72
+
73
+ Edit the class file (e.g `app/models/person.rb`):
74
+
75
+ ```ruby
76
+ class Person < ApplicationRecord
77
+ include UrlSlug
78
+ url_slug_for :name
79
+ end
80
+ ```
81
+
82
+ Now when you create a new Person:
83
+
84
+ ```ruby
85
+ person = Person.create!(name: "Ivan The Terrible")
86
+ person.name_slug #=> "ivan-the-terrible"
87
+ ```
88
+
89
+ Optionally, if you want to persist the slug to db, create a migration
90
+
91
+ ```shell
92
+ rails g migration AddNameSlugToPersons name_slug:string
93
+ rails db:migrate
94
+ ```
95
+
96
+ ### I18n support
97
+
98
+ Make sure you have locale files in place
99
+
100
+
101
+ ```ruby
102
+ I18n.with_locale(:de) do
103
+ person = Person.create!(name: "Wörter")
104
+ person.name_slug #=> "worter"
105
+ end
106
+ ```
107
+
108
+ ## Bugs
109
+
110
+ Please report them on the [Github issue
111
+ tracker](https://github.com/gorangorun/url_slug/issues) for this project.
112
+
113
+ ## License
114
+
115
+ Copyright (c) 2023 Goran Gjuroski, released under the MIT license.
116
+
117
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
118
+ this software and associated documentation files (the "Software"), to deal in
119
+ the Software without restriction, including without limitation the rights to
120
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
121
+ of the Software, and to permit persons to whom the Software is furnished to do
122
+ so, subject to the following conditions:
123
+
124
+ The above copyright notice and this permission notice shall be included in all
125
+ copies or substantial portions of the Software.
126
+
127
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
128
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
129
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
130
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
131
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
132
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
133
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/Thorfile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/generators/url_slug/install_generator'
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'url_slug'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'active_support/inflector'
5
+ require 'byebug'
6
+
7
+ module UrlSlug
8
+ module Generators
9
+ module Actions
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ argument :fields, type: :string
14
+
15
+ def include_module_into_class
16
+ puts "#{class_file_path} not found" unless class_exists?
17
+ puts "Module already included in #{class_file_path}" if class_exists? && module_included?
18
+
19
+ inject_into_file(
20
+ class_file_path,
21
+ " include UrlSlug\n",
22
+ after: /^class #{class_name}[^\n]*\n/
23
+ )
24
+ end
25
+
26
+ def inject_macro_into_class
27
+ puts "#{class_file_path} not found" unless class_exists?
28
+ puts "Macro url_slug_for already included in #{class_file_path}" if class_exists? && macro_included?
29
+
30
+ inject_into_file(
31
+ class_file_path,
32
+ build_macro,
33
+ after: /^ include UrlSlug\n/
34
+ )
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def class_file_path
41
+ "#{singular_name}.rb"
42
+ end
43
+
44
+ def class_exists?
45
+ File.exist?(class_file_path)
46
+ end
47
+
48
+ def module_included?
49
+ File.read(class_file_path).include?('include UrlSlug')
50
+ end
51
+
52
+ def macro_included?
53
+ File.read(class_file_path).include?("url_slug_for :#{slug_for}")
54
+ end
55
+
56
+ def slug_for
57
+ @slug_for ||= fields.split(':').first
58
+ end
59
+
60
+ def attr
61
+ @attr ||= fields.split(':')[1]
62
+ end
63
+
64
+ def build_macro
65
+ macro = ' url_slug_for '
66
+ macro += ":#{slug_for}"
67
+ macro += ", attr: :#{attr}" if attr.present?
68
+ macro += "\n"
69
+ macro
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'actions'
4
+
5
+ module UrlSlug
6
+ module Generators
7
+ class InstallGenerator < Thor::Group
8
+ include Thor::Actions
9
+
10
+ desc 'Install UrlSlug'
11
+ argument :klass, type: :string
12
+
13
+ include Actions
14
+
15
+ namespace 'url_slug:install'
16
+ class_option :path
17
+
18
+ private
19
+
20
+ def singular_name
21
+ klass.singularize.downcase
22
+ end
23
+
24
+ def class_name
25
+ klass
26
+ end
27
+
28
+ def class_file_path
29
+ options[:path] || super
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/named_base'
4
+ require 'rails/generators/active_record'
5
+ require_relative '../actions'
6
+
7
+ module UrlSlug
8
+ module Generators
9
+ class RailsGenerator < Rails::Generators::NamedBase
10
+ include Rails::Generators::Migration
11
+ include Actions
12
+ source_root File.expand_path('templates', __dir__)
13
+
14
+ class_option :migration, type: :boolean
15
+
16
+ def create_url_slug_migration
17
+ return unless include_migration?
18
+
19
+ if table_exists?
20
+ create_add_slug_to_table_migration
21
+ else
22
+ puts "#{table_name} table does not exists"
23
+ end
24
+ end
25
+
26
+ def self.next_migration_number(dir)
27
+ ActiveRecord::Generators::Base.next_migration_number(dir)
28
+ end
29
+
30
+ private
31
+
32
+ def create_add_slug_to_table_migration
33
+ config = {
34
+ migration_version: migration_version,
35
+ column: slug_attr,
36
+ table: table_name
37
+ }
38
+
39
+ migration_template(
40
+ 'db/migrate/add_slug_to_table.rb.erb',
41
+ File.join(migration_path, "add_#{slug_attr}_to_#{plural_name}.rb"),
42
+ config
43
+ )
44
+ end
45
+
46
+ def class_file_path
47
+ "app/models/#{super}"
48
+ end
49
+
50
+ def migration_path
51
+ 'db/migrate'
52
+ end
53
+
54
+ def include_migration?
55
+ options.key?(:migration)
56
+ end
57
+
58
+ def table_exists?
59
+ ActiveRecord::Base.connection.data_source_exists?(table_name)
60
+ end
61
+
62
+ def slug_attr
63
+ attr.present? ? attr : "#{slug_for}_slug"
64
+ end
65
+
66
+ def migration_version
67
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ class Add<%= config[:column].camelize %>To<%= config[:table].camelize %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ add_column :<%= config[:table] %>, :<%= config[:column] %>, :string
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UrlSlug
4
+ class << self
5
+ attr_accessor :configuration
6
+ end
7
+
8
+ def self.configure
9
+ self.configuration ||= Configuration.new
10
+ yield(configuration)
11
+ end
12
+
13
+ class Configuration
14
+ def i18n_load_path
15
+ ::I18n.load_path
16
+ end
17
+
18
+ def i18n_load_path=(value)
19
+ ::I18n.load_path += value
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UrlSlug
4
+ VERSION = '0.1.2'
5
+ end
data/lib/url_slug.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'url_slug/version'
4
+ require 'url_slug/configuration'
5
+ require 'active_support/inflector'
6
+ require 'active_support/concern'
7
+
8
+ module UrlSlug
9
+ include ActiveSupport::Inflector
10
+ extend ActiveSupport::Concern
11
+
12
+ class_methods do
13
+ def url_slug_for(name, args = {})
14
+ slug_name = args[:attr] || "#{name}_slug"
15
+
16
+ class_eval <<-CODE, __FILE__, __LINE__ + 1
17
+ def #{slug_name}
18
+ defined?(super) ? super : @#{slug_name}
19
+ end
20
+
21
+ def #{slug_name}=(value)
22
+ return if value.nil?
23
+ defined?(super) ? super(value.parameterize) : @#{slug_name} = value.parameterize
24
+ end
25
+
26
+ def #{name}=(value)
27
+ self.#{slug_name} = value
28
+ defined?(super) ? super(value) : @#{name} = value
29
+ end
30
+ CODE
31
+ end
32
+ end
33
+ end
data/sig/url_slug.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module UrlSlug
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'url_slug'
4
+ require 'rails/generators'
5
+ require 'generators/url_slug/install_generator'
6
+ require 'generators/url_slug/rails/rails_generator'
7
+ require 'support/generator_spec_helpers'
8
+
9
+ RSpec.configure do |config|
10
+ # Enable flags like --only-failures and --next-failure
11
+ config.example_status_persistence_file_path = '.rspec_status'
12
+
13
+ # Disable RSpec exposing methods globally on `Module` and `main`
14
+ config.disable_monkey_patching!
15
+
16
+ config.expect_with :rspec do |c|
17
+ c.syntax = :expect
18
+ end
19
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GeneratorSpecHelpers
4
+ TMP_DIR = File.expand_path('tmp')
5
+
6
+ def file(filename)
7
+ File.join(TMP_DIR, filename)
8
+ end
9
+
10
+ def file_content(filename)
11
+ File.read(file(filename))
12
+ end
13
+
14
+ def file_exists?(filename)
15
+ File.exist?(file(filename))
16
+ end
17
+
18
+ def provide_existing_class(class_file, class_name)
19
+ copy_to_generator_tmp(template_class(class_name), file(class_file))
20
+ end
21
+
22
+ def prepare_destination
23
+ FileUtils.rm_rf(Dir[File.join(TMP_DIR, '*')])
24
+ end
25
+
26
+ def capture_output
27
+ original_stdout = $stdout
28
+ $stdout = StringIO.new
29
+ yield
30
+ $stdout.string
31
+ ensure
32
+ $stdout = original_stdout
33
+ end
34
+
35
+ private
36
+
37
+ def copy_to_generator_tmp(template, destination)
38
+ FileUtils.mkdir_p(File.dirname(destination))
39
+ FileUtils.cp(template, destination)
40
+ end
41
+
42
+ def template_class(class_name)
43
+ File.expand_path("spec/support/#{class_name}")
44
+ end
45
+ end
46
+
47
+ RSpec.configure do |config|
48
+ config.include GeneratorSpecHelpers
49
+
50
+ config.after(:example, :generator) do
51
+ prepare_destination
52
+ end
53
+ end
@@ -0,0 +1,35 @@
1
+ mk:
2
+ i18n:
3
+ transliterate:
4
+ rule:
5
+ 'а': a
6
+ 'б': b
7
+ 'в': v
8
+ 'г': g
9
+ 'д': d
10
+ 'ѓ': gj
11
+ 'е': e
12
+ 'ж': zh
13
+ 'з': z
14
+ 'ѕ': s
15
+ 'и': i
16
+ 'ј': j
17
+ 'к': k
18
+ 'л': l
19
+ 'љ': lj
20
+ 'м': m
21
+ 'н': n
22
+ 'њ': nj
23
+ 'о': o
24
+ 'п': p
25
+ 'р': r
26
+ 'с': s
27
+ 'т': t
28
+ 'ќ': kj
29
+ 'у': u
30
+ 'ф': f
31
+ 'х': h
32
+ 'ц': c
33
+ 'ч': ch
34
+ 'џ': dz
35
+ 'ш': sh
data/spec/support/post ADDED
@@ -0,0 +1,2 @@
1
+ class Post
2
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe UrlSlug::Generators::InstallGenerator, :generator do
6
+ let(:generator) { described_class.new(*params) }
7
+ let(:class_name) { 'Post' }
8
+ let(:field) { 'title' }
9
+
10
+ before do
11
+ allow(generator).to receive(:class_file_path).and_return(file(class_filename))
12
+
13
+ provide_existing_class(class_filename, class_name.downcase)
14
+
15
+ capture_output { generator.invoke_all }
16
+ end
17
+
18
+ describe 'class file' do
19
+ let(:params) { [[class_name, field]] }
20
+
21
+ it 'includes UrlSlug module' do
22
+ expect(class_content).to include('include UrlSlug')
23
+ end
24
+
25
+ it 'includes macro' do
26
+ expect(class_content).to include("url_slug_for :#{field}")
27
+ end
28
+ end
29
+
30
+ def class_filename
31
+ "#{class_name.downcase}.rb"
32
+ end
33
+
34
+ def field_slug
35
+ "#{field}_slug"
36
+ end
37
+
38
+ def class_content
39
+ file_content(class_filename)
40
+ end
41
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe UrlSlug::Generators::RailsGenerator, :generator do
6
+ let(:generator) { described_class.new(*params) }
7
+ let(:class_name) { 'Post' }
8
+ let(:field) { 'title' }
9
+ let(:migration_number) { '00000000000000' }
10
+
11
+ before do
12
+ stub_const('MODEL_DIR', 'app/models')
13
+ stub_const('MIGRATION_DIR', 'db/migrate')
14
+
15
+ allow(generator).to receive(:class_file_path).and_return(file(class_path))
16
+ allow(generator).to receive(:migration_path).and_return('tmp/db/migrate')
17
+ allow(generator).to receive(:table_exists?).and_return(true)
18
+ allow(generator).to receive(:migration_version).and_return('7.0')
19
+ allow(described_class).to receive(:next_migration_number).and_return(migration_number)
20
+
21
+ provide_existing_class(class_path, class_name.downcase)
22
+
23
+ capture_output { generator.invoke_all }
24
+ end
25
+
26
+ describe 'class file' do
27
+ let(:params) { [[class_name, field]] }
28
+
29
+ xit 'check syntaxt' do
30
+ end
31
+
32
+ it 'includes UrlSlug module' do
33
+ expect(file_content(class_path)).to include('include UrlSlug')
34
+ end
35
+
36
+ it 'includes macro' do
37
+ expect(file_content(class_path)).to include("url_slug_for :#{field}")
38
+ end
39
+
40
+ it 'doesnt generate migration file' do
41
+ expect(migration_exists?).to be(false)
42
+ end
43
+ end
44
+
45
+ describe 'migration file' do
46
+ let(:params) { [[class_name, field], { migration: true }] }
47
+
48
+ it 'generates migration file' do
49
+ expect(migration_exists?).to be(true)
50
+ end
51
+
52
+ it 'contains add_column' do
53
+ result = "add_column :#{plural_name}, :#{field_slug}, :string"
54
+ expect(file_content(migration_path)).to include(result)
55
+ end
56
+ end
57
+
58
+ def plural_name
59
+ class_name.downcase.pluralize
60
+ end
61
+
62
+ def field_slug
63
+ "#{field}_slug"
64
+ end
65
+
66
+ def class_filename
67
+ "#{class_name.downcase}.rb"
68
+ end
69
+
70
+ def class_path
71
+ File.join(MODEL_DIR, class_filename)
72
+ end
73
+
74
+ def migration_filename
75
+ "#{migration_number}_add_#{field_slug}_to_#{plural_name}.rb"
76
+ end
77
+
78
+ def migration_path
79
+ File.join(MIGRATION_DIR, migration_filename)
80
+ end
81
+
82
+ def migration_exists?
83
+ file_exists?(migration_path)
84
+ end
85
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'byebug'
4
+
5
+ RSpec.describe UrlSlug do
6
+ let(:klass) do
7
+ Class.new do
8
+ include UrlSlug
9
+
10
+ attr_accessor :title
11
+
12
+ url_slug_for :title, persistence: false
13
+ end
14
+ end
15
+
16
+ let(:instance) { klass.new }
17
+
18
+ it 'has a version number' do
19
+ expect(UrlSlug::VERSION).not_to be nil
20
+ end
21
+
22
+ it 'defines the slug_name reader' do
23
+ expect(instance.respond_to?(:title_slug)).to be true
24
+ end
25
+
26
+ it 'defines the slug_name writer' do
27
+ expect(instance.respond_to?(:title_slug=)).to be true
28
+ end
29
+
30
+ it 'parameterizes slug' do
31
+ instance.title = 'foo bar'
32
+ expect(instance.title_slug).to eq('foo-bar')
33
+ end
34
+
35
+ it 'does not parameterize slug for empty string' do
36
+ expect(instance.title_slug).to eq(nil)
37
+ end
38
+
39
+ describe 'With i18n support' do
40
+ before do
41
+ described_class.configure do |config|
42
+ config.i18n_load_path = Dir[File.join('spec/support/locales', '**/*.*')]
43
+ end
44
+ end
45
+
46
+ it 'parameterizes and transliterates slug' do
47
+ I18n.available_locales = %i[en mk]
48
+
49
+ I18n.with_locale(:mk) do
50
+ instance.title = 'фоо бар'
51
+ expect(instance.title_slug).to eq('foo-bar')
52
+ end
53
+ end
54
+ end
55
+ end
data/url_slug.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/url_slug/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'url_slug'
7
+ spec.version = UrlSlug::VERSION
8
+ spec.authors = ['Goran Gjuroski']
9
+ spec.email = ['gorang.pub@gmail.com']
10
+
11
+ spec.summary = 'Yet another URL slug plugin.'
12
+ spec.description = 'UrlSlug is a slug/permalink plugin with ActiveRecord and I18n support. It lets you create SEO friendly URLs.'
13
+ spec.homepage = 'https://github.com/gorangorun/url_slug'
14
+ spec.extra_rdoc_files = %w(LICENSE README.md)
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = spec.homepage
20
+ spec.metadata['changelog_uri'] = spec.homepage
21
+
22
+ spec.files = `git ls-files`.split("\n")
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activesupport', '>= 4.0.0'
26
+ spec.add_dependency 'thor'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'i18n', '~> 1.13'
29
+ spec.add_development_dependency 'railties', '>= 4.0'
30
+ spec.add_development_dependency 'activerecord', '>= 4.0'
31
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url_slug
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Goran Gjuroski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: railties
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '4.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '4.0'
97
+ description: UrlSlug is a slug/permalink plugin with ActiveRecord and I18n support.
98
+ It lets you create SEO friendly URLs.
99
+ email:
100
+ - gorang.pub@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files:
104
+ - LICENSE
105
+ - README.md
106
+ files:
107
+ - ".gitignore"
108
+ - ".rspec"
109
+ - ".rubocop.yml"
110
+ - CHANGELOG.md
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE
115
+ - README.md
116
+ - Rakefile
117
+ - Thorfile
118
+ - bin/console
119
+ - bin/setup
120
+ - lib/generators/url_slug/actions.rb
121
+ - lib/generators/url_slug/install_generator.rb
122
+ - lib/generators/url_slug/rails/rails_generator.rb
123
+ - lib/generators/url_slug/rails/templates/db/migrate/add_slug_to_table.rb.erb
124
+ - lib/url_slug.rb
125
+ - lib/url_slug/configuration.rb
126
+ - lib/url_slug/version.rb
127
+ - sig/url_slug.rbs
128
+ - spec/spec_helper.rb
129
+ - spec/support/generator_spec_helpers.rb
130
+ - spec/support/locales/mk.yml
131
+ - spec/support/post
132
+ - spec/url_slug/generators/install_generator_spec.rb
133
+ - spec/url_slug/generators/rails_generator_spec.rb
134
+ - spec/url_slug_spec.rb
135
+ - url_slug.gemspec
136
+ homepage: https://github.com/gorangorun/url_slug
137
+ licenses:
138
+ - MIT
139
+ metadata:
140
+ homepage_uri: https://github.com/gorangorun/url_slug
141
+ source_code_uri: https://github.com/gorangorun/url_slug
142
+ changelog_uri: https://github.com/gorangorun/url_slug
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: 2.4.0
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubygems_version: 3.3.7
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Yet another URL slug plugin.
162
+ test_files: []