Skip to content

Commit

Permalink
Merge branch 'pskl-hotfix/linterminated'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fivell committed Jun 27, 2016
2 parents ab6af0a + 35b22ce commit d3bf95f
Show file tree
Hide file tree
Showing 21 changed files with 435 additions and 342 deletions.
46 changes: 46 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,46 @@
AllCops:
TargetRubyVersion: 2.3
Exclude:
- 'db/**/*'
- 'spec/fixtures/**/*'
- 'vendor/**/*'
- 'bin/*'

Style/IndentationWidth:
Width: 2

Style/Documentation:
Enabled: false

Style/Encoding:
Enabled: false

Style/MultilineOperationIndentation:
EnforcedStyle: indented

Style/FirstParameterIndentation:
EnforcedStyle: special_for_inner_method_call_in_parentheses

Metrics/AbcSize:
Max: 25

Metrics/LineLength:
Max: 120

Metrics/MethodLength:
Max: 40

Metrics/ClassLength:
Max: 250

Metrics/ModuleLength:
Max: 250

Metrics/CyclomaticComplexity:
Max: 8

Metrics/PerceivedComplexity:
Max: 8

Rails:
Enabled: true
6 changes: 5 additions & 1 deletion Gemfile
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'

# Specify your gem's dependencies in active_admin_importable.gemspec
Expand All @@ -7,7 +8,9 @@ group :test do
gem 'sprockets-rails', '2.3.3'
gem 'rails', "#{ENV['RAILS'] || default_rails_version}"
gem 'rspec-rails'
gem 'activeadmin', github: 'activeadmin' , ref: 'd787029e5523be2eb2ed99816eb0cecca2b72862'
gem 'activeadmin',
github: 'activeadmin',
ref: 'd787029e5523be2eb2ed99816eb0cecca2b72862'
gem 'coveralls', require: false # Test coverage website. Go to https://coveralls.io
gem 'sass-rails'
gem 'sqlite3'
Expand All @@ -21,4 +24,5 @@ group :test do
else
gem 'mime-types', '< 3.0.0'
end
gem 'mime-types', (RUBY_VERSION >= '2.0' ? '~> 3.0' : '~> 2.99')
end
5 changes: 3 additions & 2 deletions Rakefile
@@ -1,7 +1,8 @@
require "bundler"
# frozen_string_literal: true
require 'bundler'
require 'rake'
Bundler.setup
Bundler::GemHelper.install_tasks

# Import all our rake tasks
FileList['tasks/**/*.rake'].each { |task| import task }
FileList['tasks/**/*.rake'].each { |task| import task }
21 changes: 10 additions & 11 deletions active_admin_import.gemspec
@@ -1,25 +1,24 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true
require File.expand_path('../lib/active_admin_import/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Igor Fedoronchuk"]
gem.email = ["fedoronchuk@gmail.com"]
gem.description = "The most efficient way to import for Active Admin"
gem.summary = "ActiveAdmin import based on activerecord-import gem."
gem.homepage = "http://github.com/Fivell/active_admin_import"
gem.authors = ['Igor Fedoronchuk']
gem.email = ['fedoronchuk@gmail.com']
gem.description = 'The most efficient way to import for Active Admin'
gem.summary = 'ActiveAdmin import based on activerecord-import gem.'
gem.homepage = 'http://github.com/Fivell/active_admin_import'
gem.license = 'MIT'
gem.files = `git ls-files`.split($\)
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "active_admin_import"
gem.require_paths = ["lib"]
gem.name = 'active_admin_import'
gem.require_paths = ['lib']
gem.version = ActiveAdminImport::VERSION


gem.add_runtime_dependency 'activerecord-import', '~> 0.13.0'
gem.add_runtime_dependency 'rchardet', '~> 1.6'

gem.add_runtime_dependency 'rubyzip', '~> 1.2'
gem.add_dependency "rails", ">= 4.0"

gem.add_dependency 'rails', '>= 4.0'
end
3 changes: 1 addition & 2 deletions lib/active_admin_import.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'activerecord-import'
require 'active_admin'
require 'active_admin_import/version'
Expand All @@ -9,5 +10,3 @@
require 'active_admin_import/model'
require 'active_admin_import/authorization'
::ActiveAdmin::DSL.send(:include, ActiveAdminImport::DSL)


1 change: 1 addition & 0 deletions lib/active_admin_import/authorization.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module ActiveAdminImport
# Default Authorization permission for ActiveAdminImport
module Authorization
Expand Down
90 changes: 52 additions & 38 deletions lib/active_admin_import/dsl.rb
@@ -1,48 +1,55 @@
# frozen_string_literal: true
module ActiveAdminImport
# Declares import functionality
#
# Options
# +back+:: resource action to redirect after processing
# +csv_options+:: hash to override default CSV options
# +batch_size+:: integer value of max record count inserted by 1 query/transaction
# +batch_transaction+:: bool (false by default), if transaction is used when batch importing
# and works when :validate is set to true
# +before_import+:: proc for before import action, hook called with importer object
# +after_import+:: proc for after import action, hook called with importer object
# +before_batch_import+:: proc for before each batch action, called with importer object
# +after_batch_import+:: proc for after each batch action, called with importer object
# +validate+:: true|false, means perform validations or not
# +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import
# to use MySQL's ON DUPLICATE KEY UPDATE ability.
# +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false)
# even if record timestamps is disabled in ActiveRecord::Base
# +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
# +template+:: custom template rendering
# +template_object+:: object passing to view
# +resource_class+:: resource class name, override to import to another table (default config.resource_class)
# +resource_label+:: resource label value (default config.resource_label)
# +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
#
module DSL


# Declares import functionality
#
# Options
# +back+:: resource action to redirect after processing
# +csv_options+:: hash to override default CSV options
# +batch_size+:: integer value of max record count inserted by 1 query/transaction
# +batch_transaction+:: bool (false by default), if transaction is used when batch importing and works when :validate is set to true
# +before_import+:: proc for before import action, hook called with importer object
# +after_import+:: proc for after import action, hook called with importer object
# +before_batch_import+:: proc for before each batch action, called with importer object
# +after_batch_import+:: proc for after each batch action, called with importer object
# +validate+:: true|false, means perform validations or not
# +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
# +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
# +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
# +template+:: custom template rendering
# +template_object+:: object passing to view
# +resource_class+:: resource class name, override to import to another table (default config.resource_class)
# +resource_label+:: resource label value (default config.resource_label)
# +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
#


DEFAULT_RESULT_PROC = ->(result, options) do

DEFAULT_RESULT_PROC = lambda do |result, options|
model_name = options[:resource_label].downcase
plural_model_name = options[:plural_resource_label].downcase
if result.empty?
flash[:warning] = I18n.t('active_admin_import.file_empty_error')
else
if result.has_failed?
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))
if result.failed?
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))
return if options[:batch_transaction]
end
if result.has_imported?
flash[:notice] = I18n.t('active_admin_import.imported', count: result.imported_qty, model: model_name, plural_model: plural_model_name)
if result.imported?
flash[:notice] = I18n.t(
'active_admin_import.imported',
count: result.imported_qty,
model: model_name,
plural_model: plural_model_name)
end
end
end


# rubocop:disable Metrics/AbcSize
def active_admin_import(options = {}, &block)
options.assert_valid_keys(*Options::VALID_OPTIONS)

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

action_item :import, only: :index do
if authorized?(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
link_to(I18n.t('active_admin_import.import_model', plural_model: options[:plural_resource_label]), action: :import)
link_to(
I18n.t('active_admin_import.import_model', plural_model: options[:plural_resource_label]),
action: :import
)
end
end

Expand All @@ -66,14 +76,18 @@ def active_admin_import(options = {}, &block)

@active_admin_import_model = options[:template_object]
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
#go back to form
# go back to form
return render template: options[:template] unless @active_admin_import_model.valid?
@importer = Importer.new(options[:resource_class], @active_admin_import_model, options)
@importer = Importer.new(
options[:resource_class],
@active_admin_import_model,
options
)
begin
result = @importer.import

if block_given?
instance_eval &block
instance_eval(&block)
else
instance_exec result, options, &DEFAULT_RESULT_PROC
end
Expand All @@ -83,7 +97,7 @@ def active_admin_import(options = {}, &block)
end
redirect_to options[:back]
end

# rubocop:enable Metrics/AbcSize
end
end
end
5 changes: 2 additions & 3 deletions lib/active_admin_import/engine.rb
@@ -1,9 +1,8 @@
# frozen_string_literal: true
require 'rails'

module ActiveAdminImport
class Engine < ::Rails::Engine

config.mount_at = '/'

end
end
end
11 changes: 6 additions & 5 deletions lib/active_admin_import/import_result.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module ActiveAdminImport
class ImportResult
attr_reader :failed, :total
Expand All @@ -16,11 +17,11 @@ def imported_qty
total - failed.count
end

def has_imported?
def imported?
imported_qty > 0
end

def has_failed?
def failed?
@failed.any?
end

Expand All @@ -30,10 +31,10 @@ def empty?

def failed_message(options = {})
limit = options.fetch(:limit, failed.count)
failed.first(limit).map{|record|
failed.first(limit).map do |record|
errors = record.errors
(errors.full_messages.zip errors.keys.map{|k| record.send k}).map{|ms| ms.join(' - ')}.join(', ')
}.join(" ; ")
(errors.full_messages.zip errors.keys.map { |k| record.send k }).map { |ms| ms.join(' - ') }.join(', ')
end.join(' ; ')
end
end
end

0 comments on commit d3bf95f

Please sign in to comment.