active_permalink 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da5262296637cea151a12211bbff29a02fdf70ef
4
+ data.tar.gz: 9a9c55ff2ded150edd62fa0e9fe65de96f4e528e
5
+ SHA512:
6
+ metadata.gz: 6319685df9f33bb57a64b3e99f13575c23e427caf4427f249f6ed9c25a795cd6bfe045e7a260541f8d84424b3f0452b51f67d465f603e4cc761092ebf04d4cb5
7
+ data.tar.gz: '04388c30e998ca73cbeca70c729862207c4d21983fa22054887582571d4420d43ddfb1c0be7b99a5086af3bdeae58510cef6d8bd23179f9f1d8f2e0b0eacfbd4'
@@ -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 jonian@hardpixel.eu. 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,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in active_permalink.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jonian Guveli
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,43 @@
1
+ # ActivePermalink
2
+
3
+ Add SEO friendly permalinks to ActiveRecord model with history support.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/active_permalink.svg)](https://badge.fury.io/rb/active_permalink)
6
+ [![Build Status](https://travis-ci.org/hardpixel/active-permalink.svg?branch=master)](https://travis-ci.org/hardpixel/active-permalink)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/7669a285b9da35143c1f/maintainability)](https://codeclimate.com/github/hardpixel/active-permalink/maintainability)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'active_permalink'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install active_permalink
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`.
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/active-permalink. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the ActivePermalink project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hardpixel/active-permalink/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,18 @@
1
+ require 'active_record'
2
+ require 'active_delegate'
3
+ require 'babosa'
4
+ require 'active_permalink/version'
5
+
6
+ module ActivePermalink
7
+ extend ActiveSupport::Autoload
8
+
9
+ # Autoload base modules
10
+ autoload :Permalink
11
+ autoload :Generator
12
+ autoload :Sluggable
13
+ autoload :Loader
14
+ end
15
+
16
+ ActiveSupport.on_load(:active_record) do
17
+ include ActivePermalink::Loader
18
+ end
@@ -0,0 +1,61 @@
1
+ module ActivePermalink
2
+ class Generator
3
+ def initialize(record, field, old_value, new_value, options={})
4
+ @record = record
5
+ @field = field
6
+ @old_value = old_value
7
+ @new_value = new_value
8
+ @options = options
9
+ @translit = options[:transliterations]
10
+ @scope = options.fetch :scope, :global
11
+ end
12
+
13
+ def slug_candidates
14
+ @new_value.present? ? @new_value : @record.send(@field)
15
+ end
16
+
17
+ def slug_from_column
18
+ @column_slug ||= slug_candidates.to_slug.normalize(transliterations: @translit).to_s
19
+ end
20
+
21
+ def scope_unique_slug
22
+ unique = slug_from_column
23
+ index = 1
24
+
25
+ while not Permalink.where(scope: @scope, slug: unique).count.zero?
26
+ unique = "#{slug_from_column}-#{(index += 1)}"
27
+ end
28
+
29
+ unique
30
+ end
31
+
32
+ def deactivate_old_permalink
33
+ unless @record.active_permalink.new_record?
34
+ parameters = { slug: @old_value, active: true }
35
+ permalink = @record.old_permalinks.rewhere(parameters).first
36
+
37
+ permalink.update_column(:active, false) unless permalink.nil?
38
+ end
39
+ end
40
+
41
+ def reassign_old_permalink
42
+ permalink = @record.old_permalinks.find_by(slug: slug_from_column)
43
+
44
+ unless permalink.nil?
45
+ permalink.update(active: true)
46
+ permalink
47
+ end
48
+ end
49
+
50
+ def build_new_permalink
51
+ @record.build_active_permalink(slug: scope_unique_slug, scope: @scope, active: true)
52
+ end
53
+
54
+ def new_permalink
55
+ deactivate_old_permalink
56
+ @record.reload_active_permalink
57
+
58
+ reassign_old_permalink || build_new_permalink
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,26 @@
1
+ module ActivePermalink
2
+ module Loader
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def has_permalink(field, options={})
7
+ include ActiveDelegate
8
+ include Sluggable
9
+
10
+ assoc_opts = { as: :sluggable, class_name: 'ActivePermalink::Permalink', dependent: :destroy }
11
+ has_many :permalinks, assoc_opts
12
+ has_many :old_permalinks, -> { where active: false }, assoc_opts
13
+
14
+ has_one :active_permalink, -> { where active: true }, assoc_opts.merge(autosave: true)
15
+ delegate_attribute :slug, :string, to: :active_permalink
16
+
17
+ before_save do |record|
18
+ if slug.blank? or slug_changed?
19
+ premalink_generator = Generator.new(record, field, slug_was, slug, options)
20
+ self.active_permalink = premalink_generator.new_permalink
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ module ActivePermalink
2
+ class Permalink < ActiveRecord::Base
3
+ # Set table name
4
+ self.table_name = 'permalinks'
5
+
6
+ # Set default scope
7
+ default_scope { order created_at: :desc }
8
+
9
+ # Set custom scopes
10
+ scope :global, -> { where scope: :global }
11
+
12
+ # Belongs associations
13
+ belongs_to :sluggable, polymorphic: true, optional: true
14
+ end
15
+ end
@@ -0,0 +1,48 @@
1
+ module ActivePermalink
2
+ module Sluggable
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def find_by_slug(value)
7
+ _find_by_permalinks_slug(value)
8
+ end
9
+
10
+ def find_by_slug!(value)
11
+ _find_by_permalinks_slug(value, true)
12
+ end
13
+
14
+ private
15
+
16
+ def _find_by_permalinks_slug(value, raise_error=false)
17
+ method = raise_error ? :find_by! : :find_by
18
+ record = includes(:permalinks).send(method, permalinks: { slug: value })
19
+
20
+ unless record.nil?
21
+ record.found_by_slug = value
22
+ end
23
+
24
+ record
25
+ end
26
+ end
27
+
28
+ def found_by_slug
29
+ @found_by_slug
30
+ end
31
+
32
+ def found_by_slug=(value)
33
+ @found_by_slug = value
34
+ end
35
+
36
+ def found_by_slug?
37
+ !found_by_slug.nil?
38
+ end
39
+
40
+ def needs_redirect?
41
+ found_by_slug? and found_by_slug != slug
42
+ end
43
+
44
+ def old_slugs
45
+ @old_slugs ||= old_permalinks.pluck(:slug)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module ActivePermalink
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record'
4
+
5
+ module ActivePermalink
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ desc 'Generates migrations to add permalinks tables.'
10
+ source_root File.expand_path('../templates', __FILE__)
11
+
12
+ def create_migration_file
13
+ migration_template 'migration.rb', 'db/migrate/create_permalinks.rb'
14
+ end
15
+
16
+ def self.next_migration_number(dirname)
17
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ class CreatePermalinks < ActiveRecord::Migration[5.0]
2
+ def change
3
+ create_table :permalinks do |t|
4
+ t.string :scope, default: 'global'
5
+ t.boolean :active, default: true
6
+ t.string :slug, null: false
7
+ t.integer :sluggable_id, null: false
8
+ t.string :sluggable_type, null: false
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :permalinks, [:scope, :slug], unique: true
14
+ add_index :permalinks, [:sluggable_id, :sluggable_type], name: :index_permalinks_on_sluggable_attribute
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_permalink
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonian Guveli
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_delegate
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: babosa
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: Add SEO friendly permalinks to ActiveRecord model with history support.
98
+ email:
99
+ - jonian@hardpixel.eu
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - CODE_OF_CONDUCT.md
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/active_permalink.rb
110
+ - lib/active_permalink/generator.rb
111
+ - lib/active_permalink/loader.rb
112
+ - lib/active_permalink/permalink.rb
113
+ - lib/active_permalink/sluggable.rb
114
+ - lib/active_permalink/version.rb
115
+ - lib/generators/active_permalink/install_generator.rb
116
+ - lib/generators/active_permalink/templates/migration.rb
117
+ homepage: https://github.com/hardpixel/active-permalink
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
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.6.13
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Add permalinks to ActiveRecord models
141
+ test_files: []