Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable RuboCop #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ jobs:
strategy:
matrix:
ruby:
- 1.9
- 2.1
- 2.2
- 2.3
- 2.4
- 2.5
- 2.6
- 2.7
- 3.0
- 3.1
- '1.9'
- '2.0'
- '2.1'
- '2.2'
- '2.3'
- '2.4'
- '2.5'
- '2.6'
- '2.7'
- '3.0'
- '3.1'
experimental:
- false
include:
Expand Down
12 changes: 11 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
AllCops:
TargetRubyVersion: 1.9
DisplayCopNames: true
DisplayStyleGuide: true

Style/AlignHash:
EnforcedHashRocketStyle: table
Expand All @@ -17,15 +19,22 @@ Style/BlockDelimiters:
- it
- link

Style/Documentation:
Enabled: false

Style/Lambda:
Enabled: false
EnforcedStyle: literal

Style/LambdaCall:
EnforcedStyle: braces

Metrics/ClassLength:
Exclude:
- 'test/**/*'

Metrics/LineLength:
Enabled: false
Max: 200

Metrics/MethodLength:
Max: 15
Expand All @@ -35,3 +44,4 @@ Style/PredicateName:
- is_a?
- has_one
- has_many
- has_relationship
12 changes: 8 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ end

gemspec

case ENV["GEMS_SOURCE"]
when "local"
gem "roar", path: "../roar"
when "github"
case ENV['GEMS_SOURCE']
when 'local'
gem 'roar', path: '../roar'
when 'github'
gem 'roar', github: 'trailblazer/roar'
end

gem 'minitest-line'
gem 'minitest-reporters', '<= 1.3.0' # Note 1.3.1 is broken see https://github.com/kern/minitest-reporters/issues/267
gem 'pry'

if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.6.0')
gem 'rubocop', '~> 0.41.0' # RuboCop dropped support for Ruby 1.9 after v0.41
end

gem 'json_spec', require: false
6 changes: 3 additions & 3 deletions lib/roar/json/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def included(base)
base.send(:include, JSONAPI::Mixin)
base.type(@type)
base.property(@id_key, as: :id, render_nil: false, render_filter: ->(input, _opts) {
input.to_s
})
input.to_s
})
end
end

Expand Down Expand Up @@ -96,7 +96,7 @@ module Mixin
#
# @api private
# @see http://www.ruby-doc.org/core/Module.html#method-i-included
def self.included(base)
def self.included(base) # rubocop:disable Metrics/MethodLength
base.class_eval do
feature Roar::JSON
feature Roar::Hypermedia
Expand Down
2 changes: 1 addition & 1 deletion lib/roar/json/json_api/declarative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def has_many(name, options = {}, &block)

private

def has_relationship(name, options = {}, &block)
def has_relationship(name, options = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
resource_decorator = options.delete(:decorator) ||
options.delete(:extend) ||
Class.new(Roar::Decorator).tap { |decorator|
Expand Down
4 changes: 2 additions & 2 deletions lib/roar/json/json_api/single_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module JSONAPI
# @api private
module SingleResource
# @see Document#to_hash
def to_hash(options = {})
def to_hash(options = {}) # rubocop:disable Metrics/AbcSize
document = super(Options::Include.(options, mappings))
unwrapped = options[:wrap] == false
resource = unwrapped ? document : document['data']
Expand Down Expand Up @@ -43,7 +43,7 @@ def mappings
end

def find_id_mapping(klass)
self_id = klass.definitions.detect { |definition|
klass.definitions.detect { |definition|
definition[:as] && definition[:as].(:value) == 'id'
}.name
end
Expand Down
2 changes: 1 addition & 1 deletion lib/roar/json/json_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Roar
module JSON
module JSONAPI
VERSION = '0.0.3'
VERSION = '0.0.3' # rubocop:disable Style/MutableConstant
end
end
end
8 changes: 8 additions & 0 deletions rakelib/rubocop.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
begin
require 'rubocop/rake_task'
RuboCop::RakeTask.new(:rubocop)

task default: [:rubocop]
rescue LoadError
puts 'RuboCop not available'
end
36 changes: 33 additions & 3 deletions test/jsonapi/collection_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,39 @@
require 'json'

class JsonapiCollectionRenderTest < MiniTest::Spec
let(:article) { Article.new(1, 'Health walk', Author.new(2, '[email protected]'), Author.new('editor:1'), [Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')], [Author.new('contributor:1'), Author.new('contributor:2')]) }
let(:article2) { Article.new(2, 'Virgin Ska', Author.new('author:1'), nil, [Comment.new('comment:3', 'Cool song!')], [Author.new('contributor:1'), Author.new('contributor:2')]) }
let(:article3) { Article.new(3, 'Gramo echo', Author.new('author:1'), nil, [Comment.new('comment:4', 'Skalar')], [Author.new('contributor:1'), Author.new('contributor:2')]) }
let(:article) do
Article.new(
1,
'Health walk',
Author.new(2, '[email protected]'),
Author.new('editor:1'),
[Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')],
[Author.new('contributor:1'), Author.new('contributor:2')]
)
end

let(:article2) do
Article.new(
2,
'Virgin Ska',
Author.new('author:1'),
nil,
[Comment.new('comment:3', 'Cool song!')],
[Author.new('contributor:1'), Author.new('contributor:2')]
)
end

let(:article3) do
Article.new(
3,
'Gramo echo',
Author.new('author:1'),
nil,
[Comment.new('comment:4', 'Skalar')],
[Author.new('contributor:1'), Author.new('contributor:2')]
)
end

let(:decorator) { ArticleDecorator.for_collection.new([article, article2, article3]) }

it 'renders full document' do
Expand Down
4 changes: 2 additions & 2 deletions test/jsonapi/fieldsets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class DocumentResourceWithDifferentIdAtRoot < Roar::Decorator
let(:article) {
klass = Struct.new(:article_id, :title, :summary, :comments, :author)
klass.new(1, 'My Article', 'An interesting read.', comments,
Author.new('a:1', 'Celso', '[email protected]'))
Author.new('a:1', 'Celso', '[email protected]'))
}

let(:document) {
Expand Down Expand Up @@ -541,7 +541,7 @@ class DocumentAndRelationWithDifferentId < Roar::Decorator

it do
DocumentAndRelationWithDifferentId.new(article).to_json(include: 'comments')
.must_equal_json document
.must_equal_json document
end
end
end
4 changes: 2 additions & 2 deletions test/jsonapi/relationship_custom_naming_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class RecipeDecorator < Roar::Decorator
property :name
end

has_one :best_chef, as: "bestChefEver", extend: ChefDecorator
has_many :best_ingredients, as: "bestIngridients", extend: IngredientDecorator
has_one :best_chef, as: 'bestChefEver', extend: ChefDecorator
has_many :best_ingredients, as: 'bestIngridients', extend: IngredientDecorator
end

Recipe = Struct.new(:id, :name, :best_chef, :best_ingredients, :reviews)
Expand Down
15 changes: 12 additions & 3 deletions test/jsonapi/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
require 'json'

class JsonapiRenderTest < MiniTest::Spec
let(:article) { Article.new(1, 'Health walk', Author.new(2), Author.new('editor:1'), [Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')], [Author.new('contributor:1'),Author.new('contributor:2')]) }
let(:article) do
Article.new(
1,
'Health walk',
Author.new(2),
Author.new('editor:1'),
[Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')],
[Author.new('contributor:1'), Author.new('contributor:2')]
)
end

let(:decorator) { ArticleDecorator.new(article) }

it 'renders full document' do
Expand Down Expand Up @@ -311,7 +321,6 @@ class VisualArtistDecorator < Roar::Decorator
it { VisualArtistDecorator.for_collection.new([painter]).to_json.must_equal_json collection_document }
end


describe 'null/ empty attributes render correctly' do
class ArtistDecorator < Roar::Decorator
include Roar::JSON::JSONAPI.resource :artists
Expand All @@ -324,7 +333,7 @@ class ArtistDecorator < Roar::Decorator
property :genre, render_nil: false # tests that we can override default setting
end

link(:self) { "http://artists/#{represented.id}" }
link(:self) { "http://artists/#{represented.id}" }
end

Painter = Struct.new(:id, :name, :known_aliases, :movement, :noteable_works, :genre)
Expand Down
3 changes: 0 additions & 3 deletions test/jsonapi/representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class ArticleDecorator < Roar::Decorator
# relationships
has_one :author, class: Author, decorator: AuthorDecorator,
populator: ::Representable::FindOrInstantiate do # populator is for parsing, only.

relationship do
link(:self) { "/articles/#{represented.id}/relationships/author" }
link(:related) { "/articles/#{represented.id}/author" }
Expand All @@ -99,7 +98,6 @@ class ArticleDecorator < Roar::Decorator

has_many :comments, class: Comment, decorator: CommentDecorator,
populator: ::Representable::FindOrInstantiate do

relationship do
link(:self) { "/articles/#{represented.id}/relationships/comments" }
link(:related) { "/articles/#{represented.id}/comments" }
Expand All @@ -110,7 +108,6 @@ class ArticleDecorator < Roar::Decorator
end
end


# this relationship should be listed in relationships but no data included/sideloaded
has_many :contributors, class: Author, included: false do
type :authors
Expand Down
30 changes: 18 additions & 12 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ module JsonSpec

self.excluded_keys = []
end
module MiniTest::Assertions
def assert_equal_json(actual, expected)
assert_equal scrub(actual), scrub(expected)
end

def scrub(json, path = nil)
JsonSpec::Helpers.generate_normalized_json(
JsonSpec::Exclusion.exclude_keys(
JsonSpec::Helpers.parse_json(json, path)
)
).chomp + "\n"
module MiniTest
module Assertions
def assert_equal_json(actual, expected)
assert_equal scrub(actual), scrub(expected)
end

def scrub(json, path = nil)
JsonSpec::Helpers.generate_normalized_json(
JsonSpec::Exclusion.exclude_keys(
JsonSpec::Helpers.parse_json(json, path)
)
).chomp + "\n"
end
end
end
module Minitest::Expectations
infect_an_assertion :assert_equal_json, :must_equal_json

module Minitest
module Expectations
infect_an_assertion :assert_equal_json, :must_equal_json
end
end