mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Apply Rubocop config from Rails
This commit is contained in:
parent
2771e3225b
commit
2d116f6455
12
Gemfile
12
Gemfile
@ -1,14 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
source 'https://rubygems.org'
|
||||
source "https://rubygems.org"
|
||||
gemspec
|
||||
|
||||
group :test do
|
||||
gem 'm', '~> 1.5'
|
||||
gem 'pry', '~> 0.10'
|
||||
gem 'pry-byebug', '~> 3.4', platform: :ruby
|
||||
gem "m", "~> 1.5"
|
||||
gem "pry", "~> 0.10"
|
||||
gem "pry-byebug", "~> 3.4", platform: :ruby
|
||||
end
|
||||
|
||||
group :development, :test do
|
||||
gem 'rubocop', '>= 0.47', require: false
|
||||
gem 'yard', require: false
|
||||
gem "rubocop", ">= 0.47", require: false
|
||||
gem "yard", require: false
|
||||
end
|
||||
|
||||
56
Rakefile
56
Rakefile
@ -1,9 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
require 'English'
|
||||
require 'rake/clean'
|
||||
require "English"
|
||||
require "rake/clean"
|
||||
|
||||
GEMSPEC = 'ams.gemspec'
|
||||
require File.join(File.dirname(__FILE__), 'lib', 'ams', 'version')
|
||||
GEMSPEC = "ams.gemspec"
|
||||
require File.join(File.dirname(__FILE__), "lib", "ams", "version")
|
||||
VERSION = AMS::VERSION
|
||||
VERSION_TAG = "AMS_#{VERSION}"
|
||||
GEMPATH = "AMS-#{VERSION}.gem"
|
||||
@ -12,67 +12,67 @@ CLOBBER << GEMPATH
|
||||
|
||||
SOURCE_FILES = Rake::FileList.new(GEMSPEC)
|
||||
|
||||
rule '.gem' => '.gemspec' do
|
||||
rule ".gem" => ".gemspec" do
|
||||
sh "gem build -V #{GEMSPEC}"
|
||||
end
|
||||
|
||||
desc 'build gem'
|
||||
task build: [:clobber, SOURCE_FILES.ext('.gem')]
|
||||
desc "build gem"
|
||||
task build: [:clobber, SOURCE_FILES.ext(".gem")]
|
||||
|
||||
desc 'install gem'
|
||||
desc "install gem"
|
||||
task install: :build do
|
||||
sh "gem install #{GEMPATH}"
|
||||
end
|
||||
|
||||
desc 'uninstall gem'
|
||||
desc "uninstall gem"
|
||||
task :uninstall do
|
||||
sh 'gem uninstall -aIx AMS'
|
||||
sh "gem uninstall -aIx AMS"
|
||||
end
|
||||
|
||||
desc 'test install message'
|
||||
desc "test install message"
|
||||
task :test_install do
|
||||
gemspec = Gem::Specification.load(GEMSPEC)
|
||||
puts "You should see post install message '#{gemspec.post_install_message}' below:"
|
||||
begin
|
||||
Rake::Task['install'].invoke
|
||||
Rake::Task["install"].invoke
|
||||
ensure
|
||||
Rake::Task['uninstall'].invoke
|
||||
Rake::Task["uninstall"].invoke
|
||||
end
|
||||
puts "#{GEMSPEC} => #{GEMPATH}"
|
||||
end
|
||||
|
||||
desc 'abort when repo not clean or has uncommited code'
|
||||
desc "abort when repo not clean or has uncommited code"
|
||||
task :assert_clean_repo do
|
||||
sh 'git diff --exit-code'
|
||||
abort 'Git repo not clean' unless $CHILD_STATUS.success?
|
||||
sh 'git diff-index --quiet --cached HEAD'
|
||||
abort 'Git repo not commited' unless $CHILD_STATUS.success?
|
||||
sh "git diff --exit-code"
|
||||
abort "Git repo not clean" unless $CHILD_STATUS.success?
|
||||
sh "git diff-index --quiet --cached HEAD"
|
||||
abort "Git repo not commited" unless $CHILD_STATUS.success?
|
||||
end
|
||||
|
||||
task push_and_tag: [:build] do
|
||||
sh "gem push #{GEMPATH}"
|
||||
if $CHILD_STATUS.success?
|
||||
Rake::Task['tag_globally'].invoke
|
||||
Rake::Task["tag_globally"].invoke
|
||||
else
|
||||
abort 'tagging aborted; pushing gem failed.'
|
||||
abort "tagging aborted; pushing gem failed."
|
||||
end
|
||||
end
|
||||
|
||||
task :tag_globally do
|
||||
sh "git tag -a -m \"Version #{VERSION}\" #{VERSION_TAG}"
|
||||
STDOUT.puts "Tagged #{VERSION_TAG}."
|
||||
sh 'git push'
|
||||
sh 'git push --tags'
|
||||
sh "git push"
|
||||
sh "git push --tags"
|
||||
end
|
||||
|
||||
desc 'Release'
|
||||
desc "Release"
|
||||
task release: [:assert_clean_repo, :push_and_tag]
|
||||
|
||||
import('lib/tasks/rubocop.rake')
|
||||
import('lib/tasks/doc.rake')
|
||||
import('lib/tasks/test.rake')
|
||||
import("lib/tasks/rubocop.rake")
|
||||
import("lib/tasks/doc.rake")
|
||||
import("lib/tasks/test.rake")
|
||||
|
||||
task default: [:test, :rubocop]
|
||||
|
||||
desc 'CI test task'
|
||||
task ci: [:default, 'doc:stats']
|
||||
desc "CI test task"
|
||||
task ci: [:default, "doc:stats"]
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
# frozen_string_literal: true
|
||||
require 'ams/version'
|
||||
require 'ams/serializer'
|
||||
require "ams/version"
|
||||
require "ams/serializer"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'json'
|
||||
require "json"
|
||||
module AMS
|
||||
# Lightweight mapping of a model to a JSON API resource object
|
||||
# with attributes and relationships
|
||||
@ -38,7 +38,7 @@ module AMS
|
||||
class << self
|
||||
attr_accessor :_attributes, :_relations, :_id_field, :_type
|
||||
|
||||
def add_instance_method(body, receiver=self)
|
||||
def add_instance_method(body, receiver = self)
|
||||
cl = caller_locations[0]
|
||||
silence_warnings { receiver.module_eval body, cl.absolute_path, cl.lineno }
|
||||
end
|
||||
@ -60,7 +60,7 @@ module AMS
|
||||
super
|
||||
base._attributes = _attributes.dup
|
||||
base._relations = _relations.dup
|
||||
base._type = base.name.split('::')[-1].sub('Serializer', '').downcase
|
||||
base._type = base.name.split("::")[-1].sub("Serializer", "").downcase
|
||||
add_class_method "def class; #{base}; end", base
|
||||
add_instance_method "def id; object.id; end", base
|
||||
end
|
||||
@ -79,7 +79,7 @@ module AMS
|
||||
end
|
||||
|
||||
def attribute(attribute_name, key: attribute_name)
|
||||
fail 'ForbiddenKey' if attribute_name == :id
|
||||
fail "ForbiddenKey" if attribute_name == :id
|
||||
_attributes[attribute_name] = { key: key }
|
||||
add_instance_method <<-METHOD
|
||||
def #{attribute_name}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
# frozen_string_literal: true
|
||||
module AMS
|
||||
VERSION = '0.99.0'
|
||||
VERSION = "0.99.0"
|
||||
end
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
require 'yard'
|
||||
require "yard"
|
||||
|
||||
namespace :doc do
|
||||
desc 'start a gem server'
|
||||
desc "start a gem server"
|
||||
task :server do
|
||||
sh 'bundle exec yard server --gems'
|
||||
sh "bundle exec yard server --gems"
|
||||
end
|
||||
|
||||
desc 'use Graphviz to generate dot graph'
|
||||
desc "use Graphviz to generate dot graph"
|
||||
task :graph do
|
||||
output_file = 'doc/erd.dot'
|
||||
output_file = "doc/erd.dot"
|
||||
sh "bundle exec yard graph --protected --full --dependencies > #{output_file}"
|
||||
puts 'open doc/erd.dot if you have graphviz installed'
|
||||
puts "open doc/erd.dot if you have graphviz installed"
|
||||
end
|
||||
|
||||
YARD::Rake::YardocTask.new(:stats) do |t|
|
||||
t.stats_options = ['--list-undoc']
|
||||
t.stats_options = ["--list-undoc"]
|
||||
end
|
||||
|
||||
DOC_PATH = File.join('doc')
|
||||
DOC_PATH = File.join("doc")
|
||||
YARD::Rake::YardocTask.new(:pages) do |t|
|
||||
t.options = ['-o', DOC_PATH]
|
||||
t.options = ["-o", DOC_PATH]
|
||||
end
|
||||
end
|
||||
task doc: ['doc:pages']
|
||||
task doc: ["doc:pages"]
|
||||
|
||||
@ -1,53 +1,53 @@
|
||||
# frozen_string_literal: true
|
||||
begin
|
||||
require 'rubocop'
|
||||
require 'rubocop/rake_task'
|
||||
require "rubocop"
|
||||
require "rubocop/rake_task"
|
||||
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
||||
else
|
||||
require 'rbconfig'
|
||||
require "rbconfig"
|
||||
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
|
||||
windows_platforms = /(msdos|mswin|djgpp|mingw)/
|
||||
if RbConfig::CONFIG['host_os'] =~ windows_platforms
|
||||
desc 'No-op rubocop on Windows-- unsupported platform'
|
||||
if RbConfig::CONFIG["host_os"] =~ windows_platforms
|
||||
desc "No-op rubocop on Windows-- unsupported platform"
|
||||
task :rubocop do
|
||||
puts 'Skipping rubocop on Windows'
|
||||
puts "Skipping rubocop on Windows"
|
||||
end
|
||||
elsif defined?(::Rubinius)
|
||||
desc 'No-op rubocop to avoid rbx segfault'
|
||||
desc "No-op rubocop to avoid rbx segfault"
|
||||
task :rubocop do
|
||||
puts 'Skipping rubocop on rbx due to segfault'
|
||||
puts 'https://github.com/rubinius/rubinius/issues/3499'
|
||||
puts "Skipping rubocop on rbx due to segfault"
|
||||
puts "https://github.com/rubinius/rubinius/issues/3499"
|
||||
end
|
||||
else
|
||||
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
|
||||
patterns = [
|
||||
'Gemfile',
|
||||
'Rakefile',
|
||||
'lib/**/*.{rb,rake}',
|
||||
'config/**/*.rb',
|
||||
'app/**/*.rb',
|
||||
'test/**/*.rb'
|
||||
"Gemfile",
|
||||
"Rakefile",
|
||||
"lib/**/*.{rb,rake}",
|
||||
"config/**/*.rb",
|
||||
"app/**/*.rb",
|
||||
"test/**/*.rb"
|
||||
]
|
||||
desc 'Execute rubocop'
|
||||
desc "Execute rubocop"
|
||||
RuboCop::RakeTask.new(:rubocop) do |task|
|
||||
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
|
||||
task.formatters = ['progress']
|
||||
task.options = ["--rails", "--display-cop-names", "--display-style-guide"]
|
||||
task.formatters = ["progress"]
|
||||
task.patterns = patterns
|
||||
task.fail_on_error = true
|
||||
end
|
||||
|
||||
namespace :rubocop do
|
||||
desc 'Auto-gen rubocop config'
|
||||
desc "Auto-gen rubocop config"
|
||||
task :auto_gen_config do
|
||||
options = ['--auto-gen-config'].concat patterns
|
||||
require 'benchmark'
|
||||
options = ["--auto-gen-config"].concat patterns
|
||||
require "benchmark"
|
||||
result = 0
|
||||
cli = RuboCop::CLI.new
|
||||
time = Benchmark.realtime do
|
||||
result = cli.run(options)
|
||||
end
|
||||
puts "Finished in #{time} seconds" if cli.options[:debug]
|
||||
abort('RuboCop failed!') if result.nonzero?
|
||||
abort("RuboCop failed!") if result.nonzero?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
require 'rake/testtask'
|
||||
require "rake/testtask"
|
||||
|
||||
Rake::TestTask.new(:test) do |t|
|
||||
t.libs << 'lib'
|
||||
t.libs << 'test'
|
||||
t.pattern = 'test/**/*_test.rb'
|
||||
t.ruby_opts = ['-r./test/test_helper.rb']
|
||||
t.ruby_opts << ' -w' unless ENV['NO_WARN'] == 'true'
|
||||
t.libs << "lib"
|
||||
t.libs << "test"
|
||||
t.pattern = "test/**/*_test.rb"
|
||||
t.ruby_opts = ["-r./test/test_helper.rb"]
|
||||
t.ruby_opts << " -w" unless ENV["NO_WARN"] == "true"
|
||||
t.verbose = true
|
||||
end
|
||||
|
||||
2
test/fixtures/poro.rb
vendored
2
test/fixtures/poro.rb
vendored
@ -50,7 +50,7 @@ class PlainModel
|
||||
end
|
||||
end
|
||||
|
||||
class ParentModel< PlainModel
|
||||
class ParentModel < PlainModel
|
||||
attributes :id, :name, :description
|
||||
associations :child_models
|
||||
end
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
require 'bundler/setup'
|
||||
require 'simplecov'
|
||||
require 'minitest/autorun'
|
||||
require 'ams'
|
||||
require 'fixtures/poro'
|
||||
require "bundler/setup"
|
||||
require "simplecov"
|
||||
require "minitest/autorun"
|
||||
require "ams"
|
||||
require "fixtures/poro"
|
||||
|
||||
module AMS
|
||||
class Test < Minitest::Test
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
require "test_helper"
|
||||
|
||||
module AMS
|
||||
class Serializer
|
||||
@ -9,16 +9,16 @@ module AMS
|
||||
type :profiles
|
||||
attribute :name
|
||||
attribute :description, key: :summary
|
||||
relation :child_models, type: :comments, to: :many, ids: 'object.child_models.map(&:id)'
|
||||
relation :child_models, type: :comments, to: :many, ids: "object.child_models.map(&:id)"
|
||||
end
|
||||
|
||||
def setup
|
||||
super
|
||||
@relation = ChildModel.new(id: 2, name: 'comment')
|
||||
@relation = ChildModel.new(id: 2, name: "comment")
|
||||
@object = ParentModel.new(
|
||||
id: 1,
|
||||
name: 'name',
|
||||
description: 'description',
|
||||
name: "name",
|
||||
description: "description",
|
||||
child_models: [@relation]
|
||||
)
|
||||
@serializer_class = ParentModelSerializer
|
||||
@ -28,9 +28,9 @@ module AMS
|
||||
def test_model_instance_as_json
|
||||
expected = {
|
||||
id: 1, type: :profiles,
|
||||
attributes: {name: "name", summary: "description"},
|
||||
attributes: { name: "name", summary: "description" },
|
||||
relationships:
|
||||
{child_models: [{data: {id: 2, type: "comments"}}]}
|
||||
{ child_models: [{ data: { id: 2, type: "comments" } }] }
|
||||
}
|
||||
assert_equal expected, @serializer_instance.as_json
|
||||
end
|
||||
@ -38,9 +38,9 @@ module AMS
|
||||
def test_model_instance_to_json
|
||||
expected = {
|
||||
id: 1, type: :profiles,
|
||||
attributes: {name: "name", summary: "description"},
|
||||
attributes: { name: "name", summary: "description" },
|
||||
relationships:
|
||||
{child_models: [{data: {id: 2, type: "comments"}}]}
|
||||
{ child_models: [{ data: { id: 2, type: "comments" } }] }
|
||||
}.to_json
|
||||
assert_equal expected, @serializer_instance.to_json
|
||||
end
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
require "test_helper"
|
||||
|
||||
module AMS
|
||||
class Serializer
|
||||
@ -13,8 +13,8 @@ module AMS
|
||||
super
|
||||
@object = ParentModel.new(
|
||||
id: 1,
|
||||
name: 'name',
|
||||
description: 'description'
|
||||
name: "name",
|
||||
description: "description"
|
||||
)
|
||||
@serializer_class = ParentModelSerializer
|
||||
@serializer_instance = @serializer_class.new(@object)
|
||||
@ -22,8 +22,8 @@ module AMS
|
||||
|
||||
def test_model_instance_attributes
|
||||
expected_attributes = {
|
||||
name: 'name',
|
||||
summary: 'description'
|
||||
name: "name",
|
||||
summary: "description"
|
||||
}
|
||||
assert_equal expected_attributes, @serializer_instance.attributes
|
||||
end
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
require "test_helper"
|
||||
|
||||
module AMS
|
||||
class Serializer
|
||||
@ -10,7 +10,7 @@ module AMS
|
||||
|
||||
def setup
|
||||
super
|
||||
@object = ParentModel.new( id: 1,)
|
||||
@object = ParentModel.new(id: 1,)
|
||||
@serializer_class = ParentModelSerializer
|
||||
@serializer_instance = @serializer_class.new(@object)
|
||||
end
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
require "test_helper"
|
||||
|
||||
module AMS
|
||||
class Serializer
|
||||
class RelationshipsTest < Test
|
||||
class ParentModelSerializer < Serializer
|
||||
relation :child_models, type: :comments, to: :many, ids: 'object.child_models.map(&:id)'
|
||||
relation :child_models, type: :comments, to: :many, ids: "object.child_models.map(&:id)"
|
||||
end
|
||||
|
||||
def setup
|
||||
super
|
||||
@relation = ChildModel.new(id: 2, name: 'comment')
|
||||
@relation = ChildModel.new(id: 2, name: "comment")
|
||||
@object = ParentModel.new(
|
||||
child_models: [@relation]
|
||||
)
|
||||
@ -21,7 +21,7 @@ module AMS
|
||||
def test_model_instance_relations
|
||||
expected_relations = {
|
||||
child_models: [{
|
||||
data: {type: 'comments', id: 2 }
|
||||
data: { type: "comments", id: 2 }
|
||||
}]
|
||||
}
|
||||
assert_equal expected_relations, @serializer_instance.relations
|
||||
@ -29,7 +29,7 @@ module AMS
|
||||
|
||||
def test_model_instance_relationship_object
|
||||
expected = {
|
||||
data: {type: :bananas, id: 5 }
|
||||
data: { type: :bananas, id: 5 }
|
||||
}
|
||||
assert_equal expected, @serializer_instance.relationship_object(5, :bananas)
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user