Skip to content

Commit 191ba64

Browse files
committed
refactoring
1 parent a07a00b commit 191ba64

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

lib/active_admin_import/dsl.rb

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,9 @@ module DSL
4242

4343

4444
def active_admin_import(options = {}, &block)
45-
options.assert_valid_keys(*VALID_OPTIONS)
46-
47-
default_options = {
48-
back: {action: :import},
49-
csv_options: {},
50-
template: "admin/import",
51-
resource_class: config.resource_class,
52-
resource_label: config.resource_label,
53-
plural_resource_label: config.plural_resource_label,
54-
headers_rewrites: {}
55-
}
56-
options = default_options.deep_merge(options)
45+
options.assert_valid_keys(*Options::VALID_OPTIONS)
46+
47+
options = Options.options_for(config, options)
5748
params_key = ActiveModel::Naming.param_key(options[:template_object] || ActiveAdminImport::Model.new)
5849

5950
collection_action :import, method: :get do
@@ -75,7 +66,7 @@ def active_admin_import(options = {}, &block)
7566
collection_action :do_import, method: :post do
7667
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
7768

78-
@active_admin_import_model = options[:template_object] || ActiveAdminImport::Model.new
69+
@active_admin_import_model = options[:template_object]
7970
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
8071
#go back to form
8172
return render template: options[:template] unless @active_admin_import_model.valid?

lib/active_admin_import/model.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Model
1212
validate :file_contents_present, if: proc { |me| me.file.present? }
1313

1414

15-
before_validation :uncompress_file, if: proc { |me| me.archive? && me.allow_archive? }
16-
before_validation :encode_file, if: proc { |me| me.force_encoding? && me.file.present? }
15+
before_validation :uncompress_file, if: proc { |me| me.archive? && me.allow_archive? }
16+
before_validation :encode_file, if: proc { |me| me.force_encoding? && me.file.present? }
1717

1818
attr_reader :attributes
1919

@@ -28,12 +28,7 @@ def assign_attributes(args = {}, new_record = false)
2828
@attributes.merge!(args)
2929
@new_record = new_record
3030
args.keys.each do |key|
31-
key = key.to_sym
32-
#generate methods for instance object by attributes
33-
singleton_class.class_eval do
34-
define_method(key) { self.attributes[key] } unless method_defined? key
35-
define_method("#{key}=") { |new_value| @attributes[key] = new_value } unless method_defined? "#{key}="
36-
end
31+
define_methods_for(key.to_sym)
3732
end if args.is_a?(Hash)
3833
end
3934

@@ -82,7 +77,7 @@ def file_path
8277
def encode_file
8378
data = File.read(file_path).encode(force_encoding, invalid: :replace, undef: :replace)
8479
File.open(file_path, 'w') do |f|
85-
f.write(data)
80+
f.write(data)
8681
end
8782
end
8883

@@ -126,6 +121,28 @@ def file_type
126121
''
127122
end
128123
end
124+
125+
protected
126+
127+
def define_methods_for(attr_name)
128+
#generate methods for instance object by attributes
129+
singleton_class.class_eval do
130+
define_set_method(attr_name)
131+
define_get_method(attr_name)
132+
end
133+
end
134+
135+
136+
class <<self
137+
def define_set_method(attr_name)
138+
define_method(attr_name) { self.attributes[attr_name] } unless method_defined? attr_name
139+
end
140+
141+
def define_get_method(attr_name)
142+
define_method("#{attr_name}=") { |new_value| @attributes[attr_name] = new_value } unless method_defined? "#{attr_name}="
143+
end
144+
end
145+
129146
end
130147
end
131148

lib/active_admin_import/options.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module ActiveAdminImport
22

3-
VALID_OPTIONS = [
3+
module Options
4+
VALID_OPTIONS = [
45
:back,
56
:csv_options,
67
:validate,
@@ -16,12 +17,27 @@ module ActiveAdminImport
1617
:template_object,
1718
:resource_class,
1819
:resource_label,
19-
:plural_resource_label ,
20+
:plural_resource_label,
2021
:headers_rewrites
21-
].freeze
22+
].freeze
2223

2324

25+
def self.options_for(config, options= {})
26+
options[:template_object] = ActiveAdminImport::Model.new unless options.has_key? :template_object
2427

28+
{
29+
back: {action: :import},
30+
csv_options: {},
31+
template: "admin/import",
32+
resource_class: config.resource_class,
33+
resource_label: config.resource_label,
34+
plural_resource_label: config.plural_resource_label,
35+
headers_rewrites: {}
36+
}.deep_merge(options)
2537

2638

39+
40+
end
41+
42+
end
2743
end

spec/import_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ def upload_file!(name, ext='csv')
130130

131131
context "without headers" do
132132
context "with known csv headers" do
133-
before do
134-
allow_any_instance_of(ActiveAdminImport::Model).to receive(:csv_headers).and_return(['Name', 'Last name', 'Birthday'])
135-
end
133+
let(:options) {
134+
{template_object: ActiveAdminImport::Model.new(csv_headers: ['Name', 'Last name', 'Birthday'])}
135+
}
136136

137137
it "should import file" do
138138
upload_file!(:authors_no_headers)

0 commit comments

Comments
 (0)