Skip to content

Commit 798d564

Browse files
committed
Clean up
Use Rubocop linter recommendations
1 parent 6a78e34 commit 798d564

22 files changed

+390
-343
lines changed

.hound.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

.rubocop.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
AllCops:
2+
TargetRubyVersion: 2.3
3+
Exclude:
4+
- 'spec/fixtures/**/*'
5+
6+
Style/IndentationWidth:
7+
Width: 2
8+
9+
Style/Documentation:
10+
Enabled: false
11+
12+
Style/Encoding:
13+
Enabled: false
14+
15+
Style/MultilineOperationIndentation:
16+
EnforcedStyle: indented
17+
18+
Style/FirstParameterIndentation:
19+
EnforcedStyle: special_for_inner_method_call_in_parentheses
20+
21+
Metrics/AbcSize:
22+
Max: 25
23+
24+
Metrics/LineLength:
25+
Max: 120
26+
27+
Metrics/MethodLength:
28+
Max: 40
29+
30+
Metrics/ClassLength:
31+
Max: 250
32+
33+
Metrics/ModuleLength:
34+
Max: 250
35+
36+
Metrics/CyclomaticComplexity:
37+
Max: 8
38+
39+
Metrics/PerceivedComplexity:
40+
Max: 8
41+
42+
Rails:
43+
Enabled: true

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
source 'https://rubygems.org'
23

34
# Specify your gem's dependencies in active_admin_importable.gemspec
@@ -7,7 +8,6 @@ rails_version = ENV['RAILS'] || default_rails_version
78
rails_major = rails_version[0]
89

910
group :test do
10-
1111
gem 'rails', rails_version
1212
if rails_major == '5'
1313
gem 'inherited_resources', github: 'activeadmin/inherited_resources'
@@ -20,6 +20,7 @@ group :test do
2020
gem 'coveralls', require: false # Test coverage website. Go to https://coveralls.io
2121
gem 'sqlite3'
2222
gem 'launchy'
23+
gem 'pry'
2324
gem 'database_cleaner'
2425
gem 'capybara'
2526
gem 'selenium-webdriver'

Rakefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
require "bundler"
1+
# frozen_string_literal: true
2+
require 'bundler'
23
require 'rake'
34
Bundler.setup
45
Bundler::GemHelper.install_tasks
56

67
# Import all our rake tasks
7-
FileList['tasks/**/*.rake'].each { |task| import task }
8+
FileList['tasks/**/*.rake'].each { |task| import task }

active_admin_import.gemspec

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
# -*- encoding: utf-8 -*-
2+
# frozen_string_literal: true
23
require File.expand_path('../lib/active_admin_import/version', __FILE__)
34

45
Gem::Specification.new do |gem|
5-
gem.authors = ["Igor Fedoronchuk"]
6-
gem.email = ["[email protected]"]
7-
gem.description = "The most efficient way to import for Active Admin"
8-
gem.summary = "ActiveAdmin import based on activerecord-import gem."
9-
gem.homepage = "http://github.com/Fivell/active_admin_import"
6+
gem.authors = ['Igor Fedoronchuk']
7+
gem.email = ['[email protected]']
8+
gem.description = 'The most efficient way to import for Active Admin'
9+
gem.summary = 'ActiveAdmin import based on activerecord-import gem.'
10+
gem.homepage = 'http://github.com/Fivell/active_admin_import'
1011
gem.license = 'MIT'
11-
gem.files = `git ls-files`.split($\)
12+
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
1213
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
1314
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14-
gem.name = "active_admin_import"
15-
gem.require_paths = ["lib"]
15+
gem.name = 'active_admin_import'
16+
gem.require_paths = ['lib']
1617
gem.version = ActiveAdminImport::VERSION
17-
18-
19-
gem.add_runtime_dependency 'activerecord-import', '>= 0.14'
18+
gem.add_runtime_dependency 'activerecord-import', '~> 0.14.0'
2019
gem.add_runtime_dependency 'rchardet', '~> 1.6'
21-
2220
gem.add_runtime_dependency 'rubyzip', '~> 1.2'
23-
gem.add_dependency "rails", ">= 4.0"
24-
21+
gem.add_dependency 'rails', '>= 4.0'
2522
end

lib/active_admin_import.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
require 'activerecord-import'
23
require 'active_admin'
34
require 'active_admin_import/version'
@@ -9,5 +10,3 @@
910
require 'active_admin_import/model'
1011
require 'active_admin_import/authorization'
1112
::ActiveAdmin::DSL.send(:include, ActiveAdminImport::DSL)
12-
13-

lib/active_admin_import/authorization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
module ActiveAdminImport
23
# Default Authorization permission for ActiveAdminImport
34
module Authorization

lib/active_admin_import/dsl.rb

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,55 @@
1+
# frozen_string_literal: true
12
module ActiveAdminImport
3+
# Declares import functionality
4+
#
5+
# Options
6+
# +back+:: resource action to redirect after processing
7+
# +csv_options+:: hash to override default CSV options
8+
# +batch_size+:: integer value of max record count inserted by 1 query/transaction
9+
# +batch_transaction+:: bool (false by default), if transaction is used when batch importing
10+
# and works when :validate is set to true
11+
# +before_import+:: proc for before import action, hook called with importer object
12+
# +after_import+:: proc for after import action, hook called with importer object
13+
# +before_batch_import+:: proc for before each batch action, called with importer object
14+
# +after_batch_import+:: proc for after each batch action, called with importer object
15+
# +validate+:: true|false, means perform validations or not
16+
# +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import
17+
# to use MySQL's ON DUPLICATE KEY UPDATE ability.
18+
# +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false)
19+
# even if record timestamps is disabled in ActiveRecord::Base
20+
# +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
21+
# +template+:: custom template rendering
22+
# +template_object+:: object passing to view
23+
# +resource_class+:: resource class name, override to import to another table (default config.resource_class)
24+
# +resource_label+:: resource label value (default config.resource_label)
25+
# +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
26+
#
227
module DSL
3-
4-
5-
# Declares import functionality
6-
#
7-
# Options
8-
# +back+:: resource action to redirect after processing
9-
# +csv_options+:: hash to override default CSV options
10-
# +batch_size+:: integer value of max record count inserted by 1 query/transaction
11-
# +batch_transaction+:: bool (false by default), if transaction is used when batch importing and works when :validate is set to true
12-
# +before_import+:: proc for before import action, hook called with importer object
13-
# +after_import+:: proc for after import action, hook called with importer object
14-
# +before_batch_import+:: proc for before each batch action, called with importer object
15-
# +after_batch_import+:: proc for after each batch action, called with importer object
16-
# +validate+:: true|false, means perform validations or not
17-
# +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
18-
# +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
19-
# +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
20-
# +template+:: custom template rendering
21-
# +template_object+:: object passing to view
22-
# +resource_class+:: resource class name, override to import to another table (default config.resource_class)
23-
# +resource_label+:: resource label value (default config.resource_label)
24-
# +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
25-
#
26-
27-
28-
DEFAULT_RESULT_PROC = ->(result, options) do
29-
28+
DEFAULT_RESULT_PROC = lambda do |result, options|
3029
model_name = options[:resource_label].downcase
3130
plural_model_name = options[:plural_resource_label].downcase
3231
if result.empty?
3332
flash[:warning] = I18n.t('active_admin_import.file_empty_error')
3433
else
35-
if result.has_failed?
36-
flash[:error] = I18n.t('active_admin_import.failed', count: result.failed.count, model: model_name, plural_model: plural_model_name, message: result.failed_message(limit: 5))
34+
if result.failed?
35+
flash[:error] = I18n.t(
36+
'active_admin_import.failed',
37+
count: result.failed.count,
38+
model: model_name,
39+
plural_model: plural_model_name,
40+
message: result.failed_message(limit: 5))
3741
return if options[:batch_transaction]
3842
end
39-
if result.has_imported?
40-
flash[:notice] = I18n.t('active_admin_import.imported', count: result.imported_qty, model: model_name, plural_model: plural_model_name)
43+
if result.imported?
44+
flash[:notice] = I18n.t(
45+
'active_admin_import.imported',
46+
count: result.imported_qty,
47+
model: model_name,
48+
plural_model: plural_model_name)
4149
end
4250
end
4351
end
44-
45-
52+
# rubocop:disable Metrics/AbcSize
4653
def active_admin_import(options = {}, &block)
4754
options.assert_valid_keys(*Options::VALID_OPTIONS)
4855

@@ -57,7 +64,10 @@ def active_admin_import(options = {}, &block)
5764

5865
action_item :import, only: :index do
5966
if authorized?(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
60-
link_to(I18n.t('active_admin_import.import_model', plural_model: options[:plural_resource_label]), action: :import)
67+
link_to(
68+
I18n.t('active_admin_import.import_model', plural_model: options[:plural_resource_label]),
69+
action: :import
70+
)
6171
end
6272
end
6373

@@ -67,24 +77,28 @@ def active_admin_import(options = {}, &block)
6777
params = ActiveSupport::HashWithIndifferentAccess.new _params
6878
@active_admin_import_model = options[:template_object]
6979
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
70-
#go back to form
80+
# go back to form
7181
return render template: options[:template] unless @active_admin_import_model.valid?
72-
@importer = Importer.new(options[:resource_class], @active_admin_import_model, options)
82+
@importer = Importer.new(
83+
options[:resource_class],
84+
@active_admin_import_model,
85+
options
86+
)
7387
begin
7488
result = @importer.import
7589

7690
if block_given?
77-
instance_eval &block
91+
instance_eval(&block)
7892
else
7993
instance_exec result, options, &DEFAULT_RESULT_PROC
8094
end
81-
rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ActiveRecord::StatementInvalid, CSV::MalformedCSVError => e
95+
rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ActiveRecord::StatementInvalid, CSV::MalformedCSVError => e
8296
Rails.logger.error(I18n.t('active_admin_import.file_error', message: e.message))
8397
flash[:error] = I18n.t('active_admin_import.file_error', message: e.message[0..200])
8498
end
8599
redirect_to options[:back]
86100
end
87-
101+
# rubocop:enable Metrics/AbcSize
88102
end
89103
end
90104
end

lib/active_admin_import/engine.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
# frozen_string_literal: true
12
require 'rails'
23

34
module ActiveAdminImport
45
class Engine < ::Rails::Engine
5-
66
config.mount_at = '/'
7-
87
end
9-
end
8+
end

lib/active_admin_import/import_result.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
module ActiveAdminImport
23
class ImportResult
34
attr_reader :failed, :total
@@ -16,12 +17,12 @@ def imported_qty
1617
total - failed.count
1718
end
1819

19-
def has_imported?
20+
def imported?
2021
imported_qty > 0
2122
end
2223

23-
def has_failed?
24-
@failed.any?
24+
def failed?
25+
failed.any?
2526
end
2627

2728
def empty?
@@ -30,10 +31,10 @@ def empty?
3031

3132
def failed_message(options = {})
3233
limit = options.fetch(:limit, failed.count)
33-
failed.first(limit).map{|record|
34+
failed.first(limit).map do |record|
3435
errors = record.errors
35-
(errors.full_messages.zip errors.keys.map{|k| record.send k}).map{|ms| ms.join(' - ')}.join(', ')
36-
}.join(" ; ")
36+
(errors.full_messages.zip errors.keys.map { |k| record.send k }).map { |ms| ms.join(' - ') }.join(', ')
37+
end.join(' ; ')
3738
end
3839
end
3940
end

0 commit comments

Comments
 (0)