Skip to content

Commit

Permalink
Modernize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Apr 23, 2024
1 parent 3eed90b commit 7610ecc
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 25 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
name: test

on:
pull_request_target:
push:
branches:
- main
workflow_dispatch:
inputs: {}

jobs:
test:
name: test
runs-on: "ubuntu-latest"
if: |
github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target' ||
github.actor != 'dependabot[bot]'
strategy:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1"]

steps:
- uses: actions/checkout@v4

- uses: actions/cache@v4
with:
path: vendor/bundle
key: >
${{ runner.os }}-${{ matrix.ruby }}-gems-${{
hashFiles(matrix.gemfile).lock }}
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

- name: Install Ruby dependencies
run: |
bundle config path vendor/bundle
bundle config set --local frozen 'true'
bundle install
- name: Run tests
run: |
bundle exec rake
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ inherit_gem:
rubocop-fnando: .rubocop.yml

AllCops:
TargetRubyVersion: 2.7
TargetRubyVersion: 3.2
NewCops: enable

Metrics/MethodLength:
Enabled: false

Metrics/ClassLength:
Enabled: false

Minitest/EmptyLineBeforeAssertionMethods:
Enabled: false
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Permalink

[![Code Climate](https://codeclimate.com/github/fnando/permalink.svg)](https://codeclimate.com/github/fnando/permalink)
[![Build Status](https://travis-ci.org/fnando/permalink.svg)](https://travis-ci.org/fnando/permalink)
[![test](https://github.com/fnando/permalink/actions/workflows/test.yml/badge.svg)](https://github.com/fnando/permalink/actions/workflows/test.yml)
[![Gem](https://img.shields.io/gem/v/permalink.svg)](https://rubygems.org/gems/permalink)
[![Gem](https://img.shields.io/gem/dt/permalink.svg)](https://rubygems.org/gems/permalink)

Expand Down
16 changes: 13 additions & 3 deletions lib/permalink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,30 @@ module Permalink
Normalizations::LeadingTrailingDashes
].freeze

DEFAULT_SEPARATOR = "-"

DEFAULT_OPTIONS = {
normalizations: DEFAULT_NORMALIZATIONS,
separator: "-"
separator: DEFAULT_SEPARATOR
}.freeze

def self.generate(input, options = DEFAULT_OPTIONS)
options = DEFAULT_OPTIONS.merge(options)
def self.generate(
input,
normalizations: DEFAULT_NORMALIZATIONS,
separator: DEFAULT_SEPARATOR
)
options = DEFAULT_OPTIONS.merge(normalizations:, separator:)

options[:normalizations].each do |normalization|
input = normalization.call(input, options)
end

input
end

class << self
alias call generate
end
end

ActiveRecord::Base.include Permalink::ActiveRecord
4 changes: 2 additions & 2 deletions lib/permalink/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def to_param
private def create_permalink
return unless update_permalink?

permalink = Permalink.generate(
permalink = Permalink.call(
from_permalink_value.to_s,
permalink_generator_options
**permalink_generator_options
)

write_attribute(
Expand Down
2 changes: 1 addition & 1 deletion lib/permalink/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module Version
MAJOR = 2
MINOR = 1
PATCH = 0
STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
STRING = "#{MAJOR}.#{MINOR}.#{PATCH}".freeze
end
end
6 changes: 3 additions & 3 deletions permalink.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Gem::Specification.new do |s|
s.summary = "Generate permalink attributes on ActiveModel/ActiveRecord"
s.description = s.summary
s.license = "MIT"
s.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
s.required_ruby_version = Gem::Requirement.new(">= 3.2")
s.metadata["rubygems_mfa_required"] = "true"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`
.split("\n")
.map {|f| File.basename(f) }
Expand All @@ -27,5 +27,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "rubocop"
s.add_development_dependency "rubocop-fnando"
s.add_development_dependency "simplecov"
s.add_development_dependency "sqlite3"
s.add_development_dependency "sqlite3", "~> 1.4"
end
6 changes: 3 additions & 3 deletions test/permalink/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ActiveRecordTest < Minitest::Test
end

test "responds to options" do
assert model.respond_to?(:permalink_options)
assert_respond_to model, :permalink_options
end

test "uses default options" do
Expand Down Expand Up @@ -63,10 +63,10 @@ class ActiveRecordTest < Minitest::Test
another_user = User.create!

# Create posts for user
record = model.create(title: "Some nice post", user: user)
record = model.create(title: "Some nice post", user:)
assert_equal "some-nice-post", record.permalink

record = model.create(title: "Some nice post", user: user)
record = model.create(title: "Some nice post", user:)
assert_equal "some-nice-post-2", record.permalink

# Create posts for another user
Expand Down
3 changes: 2 additions & 1 deletion test/permalink/permalink_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class PermalinkTest < Minitest::Test
{
"This IS a Tripped out title!!.!1 (well/ not really)" => "this-is-a-tripped-out-title-1-well-not-really", # rubocop:disable Layout/LineLength
'////// meph1sto r0x ! \\\\\\' => "meph1sto-r0x",
"////// meph1sto r0x ! \\\\\\" => "meph1sto-r0x",
"āčēģīķļņū" => "acegiklnu",
"中文測試 chinese text" => "chinese-text",
'some-)()()-ExtRa!/// .data==?> to \/\/test' => "some-extra-data-to-test",
Expand All @@ -17,6 +17,7 @@ class PermalinkTest < Minitest::Test
}.each do |from, to|
test "creates permalink for #{from}" do
assert_equal to, Permalink.generate(from)
assert_equal to, Permalink.call(from)
end
end
end
3 changes: 1 addition & 2 deletions test/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
t.belongs_to :user
end

create_table :users do |t|
end
create_table :users
end

0 comments on commit 7610ecc

Please sign in to comment.