Skip to content

Commit

Permalink
Update styles based on rubocop warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Mar 31, 2018
1 parent 2f23482 commit 5399685
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 46 deletions.
23 changes: 19 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,32 @@ Metrics/CyclomaticComplexity:
Max: 10
Metrics/PerceivedComplexity:
Max: 10
Metrics/BlockLength:
Enabled: false
Documentation:
Enabled: false
Style/SignalException:
EnforcedStyle: only_raise
Style/IndentHash:
Layout/IndentHash:
EnforcedStyle: consistent
Style/IndentArray:
Layout/IndentArray:
EnforcedStyle: consistent
Lint/EndAlignment:
AlignWith: variable
Layout/EndAlignment:
EnforcedStyleAlignWith: variable
Lint/InheritException:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
RSpec/AnyInstance:
Enabled: false
RSpec/NestedGroups:
Max: 4
RSpec/ContextWording:
Prefixes:
- when
- with
- without
- for
- as
RSpec/MultipleExpectations:
Max: 2
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ require 'rubocop/rake_task'
RuboCop::RakeTask.new

task(:default).clear
task default: [:spec, :rubocop]
task default: %i[spec rubocop]
11 changes: 5 additions & 6 deletions acts_as_hashids.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'acts_as_hashids/version'

Expand All @@ -22,15 +21,15 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ['>= 2.2.2', '< 2.6']

spec.add_runtime_dependency 'hashids', '~> 1.0'
spec.add_runtime_dependency 'activerecord', '>= 4.0', '< 6.0'
spec.add_runtime_dependency 'hashids', '~> 1.0'

spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.5'
spec.add_development_dependency 'coveralls', '~> 0.8'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'sqlite3', '~> 1.3'
spec.add_development_dependency 'rubocop', '~> 0.54.0'
spec.add_development_dependency 'rubocop-rspec', '~> 1.24.0'
spec.add_development_dependency 'simplecov', '~> 0.11'
spec.add_development_dependency 'coveralls', '~> 0.8'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.5'
spec.add_development_dependency 'sqlite3', '~> 1.3'
end
50 changes: 31 additions & 19 deletions spec/acts_as_hashids/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,33 @@ class CoreBar < Base
end

describe '.find' do
subject { CoreFoo }
subject(:model) { CoreFoo }

let!(:foo1) { CoreFoo.create }
let!(:foo2) { CoreFoo.create }

context 'for single argument' do
it 'decodes hash id and returns the record' do
expect(subject.find(foo1.to_param)).to eq foo1
expect(model.find(foo1.to_param)).to eq foo1
end
context 'with unexisting hash id' do
it 'raises an exception' do
expect { subject.find('GXbMabNA') }.to raise_error(
expect { model.find('GXbMabNA') }.to raise_error(
ActiveRecord::RecordNotFound, "Couldn't find CoreFoo with 'id'=\"GXbMabNA\""
)
end
end
it 'returns the record when finding by string id' do
expect(subject.find(foo1.id.to_s)).to eq foo1
expect(model.find(foo1.id.to_s)).to eq foo1
end
end
context 'for multiple arguments' do
it 'decodes hash id and returns the record' do
expect(subject.find([foo1.to_param, foo2.to_param])).to eq [foo1, foo2]
expect(model.find([foo1.to_param, foo2.to_param])).to eq [foo1, foo2]
end
context 'with unexisting hash id' do
it 'raises an exception' do
expect { subject.find(%w(GXbMabNA ePQgabdg)) }.to raise_error(
expect { model.find(%w[GXbMabNA ePQgabdg]) }.to raise_error(
ActiveRecord::RecordNotFound,
"Couldn't find all CoreFoos with 'id': (\"GXbMabNA\", \"ePQgabdg\") (found 1 results, but was looking for 2)"
)
Expand All @@ -64,49 +66,59 @@ class CoreBar < Base
end
context 'as ActiveRecord_Relation' do
it 'decodes hash id and returns the record' do
expect(subject.where(nil).find(foo1.to_param)).to eq foo1
expect(model.where(nil).find(foo1.to_param)).to eq foo1
end
end
context 'as ActiveRecord_Associations_CollectionProxy' do
let!(:bar3) { CoreBar.create core_foo: foo1 }
let!(:bar4) { CoreBar.create core_foo: foo1 }
let(:bar3) { CoreBar.create core_foo: foo1 }
let(:bar4) { CoreBar.create core_foo: foo1 }

before do
bar3
bar4
end

it 'decodes hash id and returns the record' do
expect(foo1.core_bars.find(bar3.to_param)).to eq bar3
end
context 'without arguments' do
it 'delegates to detect method' do
expect(foo1.core_bars).to receive(:detect).once.and_call_original
allow(foo1.core_bars).to receive(:detect).once.and_call_original
expect(foo1.core_bars.find { |bar| bar == bar3 }).to eq bar3
expect(foo1.core_bars).to have_received(:detect).once
end
end
end
context 'reloaded' do
subject { CoreFoo.create }
context 'when reloaded' do
subject(:model) { CoreFoo.create }

it 'decodes hash id and returns the record' do
expect do
subject.reload
model.reload
end.not_to raise_error
end
end
end
describe '.with_hashids' do
subject { CoreFoo }
subject(:model) { CoreFoo }

let!(:foo1) { CoreFoo.create }
let!(:foo2) { CoreFoo.create }

it 'decodes hash id and returns the record' do
expect(subject.with_hashids([foo1.to_param, foo2.to_param]).all).to eq [foo1, foo2]
expect(model.with_hashids([foo1.to_param, foo2.to_param]).all).to eq [foo1, foo2]
end
context 'with invalid hash id' do
it 'raises an exception' do
expect { subject.with_hashids('@').all }.to raise_error(ActsAsHashids::Exception, 'Decode error: ["@"]')
expect { model.with_hashids('@').all }.to raise_error(ActsAsHashids::Exception, 'Decode error: ["@"]')
end
end
end
describe '#to_param' do
subject { CoreFoo.create }
subject(:model) { CoreFoo.create id: 5 }

it 'returns hash id' do
allow(subject).to receive(:id).and_return 5
expect(subject.to_param).to eq 'GXbMabNA'
expect(model.to_param).to eq 'GXbMabNA'
end
end
end
37 changes: 22 additions & 15 deletions spec/acts_as_hashids/methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,53 @@ def create_model(name, options = {})
end

describe '.hashids_secret' do
subject { create_model 'MethodsFoo' }
subject(:model) { create_model 'MethodsFoo' }

it 'returns the class name' do
expect(subject.hashids_secret).to eq 'MethodsFoo'
expect(model.hashids_secret).to eq 'MethodsFoo'
end
context 'for STI' do
let!(:bar) { create_model 'MethodsBar' }
subject { create_model 'MethodsFoo', base: MethodsBar }
subject(:model) { create_model 'MethodsFoo', base: MethodsBar }

it 'returns the base class name' do
expect(subject.hashids_secret).to eq 'MethodsBar'
create_model 'MethodsBar'
expect(model.hashids_secret).to eq 'MethodsBar'
end
end
context 'with custom secret' do
subject { create_model 'MethodsFoo', secret: '^_^' }
subject(:model) { create_model 'MethodsFoo', secret: '^_^' }

it 'returns the custom secret' do
expect(subject.hashids_secret).to eq '^_^'
expect(model.hashids_secret).to eq '^_^'
end
context 'with executable secret' do
subject { create_model 'MethodsFoo', secret: -> { "#{self.name} ^_^" } }
subject(:model) { create_model 'MethodsFoo', secret: -> { "#{name} ^_^" } }

it 'returns the custom secret' do
expect(subject.hashids_secret).to eq 'MethodsFoo ^_^'
expect(model.hashids_secret).to eq 'MethodsFoo ^_^'
end
end
end
end

describe '.hashids' do
subject { create_model 'MethodsFoo' }
subject(:model) { create_model 'MethodsFoo' }

it 'returns the hashids instance' do
expect(subject.hashids.encode(1)).to eq Hashids.new('MethodsFoo', 8).encode(1)
expect(model.hashids.encode(1)).to eq Hashids.new('MethodsFoo', 8).encode(1)
end
context 'with custom length' do
subject { create_model 'MethodsFoo', length: 16 }
subject(:model) { create_model 'MethodsFoo', length: 16 }

it 'returns the hashids instance' do
expect(subject.hashids.encode(1)).to eq Hashids.new('MethodsFoo', 16).encode(1)
expect(model.hashids.encode(1)).to eq Hashids.new('MethodsFoo', 16).encode(1)
end
end
context 'with custom alphabet' do
subject { create_model 'MethodsFoo', alphabet: '1234567890abcdef' }
subject(:model) { create_model 'MethodsFoo', alphabet: '1234567890abcdef' }

it 'returns the hashids instance' do
expect(subject.hashids.encode(1)).to eq Hashids.new('MethodsFoo', 8, '1234567890abcdef').encode(1)
expect(model.hashids.encode(1)).to eq Hashids.new('MethodsFoo', 8, '1234567890abcdef').encode(1)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)

# Test Coverage
require 'codeclimate-test-reporter'
Expand Down

0 comments on commit 5399685

Please sign in to comment.