awesome_imports 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in awesome_imports.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Akihiro Matsumura
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = awesome_imports
2
+
3
+ Rails plugin for import data via CSV
4
+
5
+ == Installation
6
+
7
+ gem install awesome_imports
8
+
9
+ == Usage
10
+
11
+ Later
12
+
13
+ view spec/face_app.rb
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Akihiro Matsumura. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "awesome_imports/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "awesome_imports"
7
+ s.version = AwesomeImports::VERSION
8
+ s.authors = ["Akihiro Matsumura"]
9
+ s.email = ["matsumura.aki@gmail.com"]
10
+ s.homepage = "https://github.com/mataki/awesome_imports"
11
+ s.summary = %q{Awesome csv importer for rails}
12
+ s.description = %q{Cool and simple data importer for rails by CSV}
13
+
14
+ s.rubyforge_project = "awesome_imports"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'activesupport', ['>= 3.0.0']
22
+ s.add_dependency 'activemodel', ['>= 3.0.0']
23
+ s.add_dependency 'actionpack', ['>= 3.0.0']
24
+ s.add_development_dependency 'rake', [">= 0"]
25
+ s.add_development_dependency 'rspec', ['>= 0']
26
+ end
@@ -0,0 +1,99 @@
1
+ require "csv"
2
+ require "active_model"
3
+ require "active_support/core_ext"
4
+
5
+ module AwesomeImports
6
+ class CsvImport
7
+ extend ActiveModel::Naming
8
+ include ActiveModel::Conversion
9
+ include ActiveModel::AttributeMethods
10
+
11
+ def persisted?
12
+ csv_file.present? or stored_csv_path.present?
13
+ end
14
+
15
+ def id
16
+ end
17
+
18
+ define_attribute_methods ["csv_file"]
19
+ attr_accessor :csv_file
20
+
21
+ def initialize(attr = {})
22
+ attribute = attr.with_indifferent_access
23
+ self.csv_file = attribute[:csv_file]
24
+ end
25
+
26
+ def confirm
27
+ @records = parse_csv_from_uploaded_file
28
+ records.each{ |record| record.valid? }
29
+ records
30
+ end
31
+
32
+ def parse_csv_from_uploaded_file
33
+ parse_csv(csv_file.tempfile)
34
+ end
35
+
36
+ def parse_csv(file_path)
37
+ rows = []
38
+ CSV.foreach(file_path, :encoding => "utf-8") do |row|
39
+ if respond_to?(:parse_row)
40
+ rows << parse_row(row)
41
+ else
42
+ rows << row
43
+ end
44
+ end
45
+ rows
46
+ end
47
+
48
+ def records
49
+ @records || []
50
+ end
51
+
52
+ def store_attached_csv_file
53
+ FileUtils.cp(csv_file.tempfile.path, csv_tmp_dir)
54
+ File.join(csv_tmp_dir, File.basename(csv_file.tempfile.path))
55
+ end
56
+
57
+ def csv_tmp_dir
58
+ dir = File.join(Rails.root, "tmp", "csv_import")
59
+ FileUtils.mkdir_p(dir)
60
+ dir
61
+ end
62
+
63
+ def self.restore_from_file(file_path)
64
+ res = new
65
+ res.load_stored_csv(file_path)
66
+ res
67
+ end
68
+
69
+ def load_stored_csv(file_path)
70
+ @stored_csv_path = file_path
71
+ end
72
+ attr_reader :stored_csv_path
73
+
74
+ def parse_csv_from_stored_file
75
+ parse_csv(stored_csv_path)
76
+ end
77
+
78
+ def update
79
+ @records = parse_csv_from_stored_file
80
+ Keyword.transaction do
81
+ records.each do |record|
82
+ record.save!
83
+ end
84
+ end
85
+ FileUtils.rm(stored_csv_path)
86
+ remove_old_stored_csvs
87
+ true
88
+ rescue ActiveRecord::RecordInvalid => e
89
+ records.each{ |record| record.valid? }
90
+ false
91
+ end
92
+
93
+ private
94
+ # TODO: Remove old csv files
95
+ def remove_old_stored_csvs
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,46 @@
1
+ # -*- coding: utf-8 -*-
2
+ module AwesomeImports
3
+ module CsvImportsController
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ cattr_reader :object_name, :import_name, :resource_class
8
+
9
+ @@object_name = self.name.underscore.split('_').first # "keyword"
10
+ @@import_name = [object_name, "import"].join('_') # "keyword_import"
11
+ @@resource_class = import_name.classify.constantize # KeywordImport
12
+ end
13
+
14
+ module InstanceMethods
15
+
16
+ def new
17
+ instance_variable_set("@#{import_name}", resource_class.new)
18
+ end
19
+
20
+ def create
21
+ import = resource_class.new(params[import_name.to_sym])
22
+ import.confirm
23
+ session[import_name.to_sym] = import.store_attached_csv_file
24
+
25
+ instance_variable_set("@#{import_name}", import)
26
+ end
27
+
28
+ def update
29
+ import = resource_class.restore_from_file(session[import_name.to_sym])
30
+ if import.update
31
+ flash[:notice] = I18n.t("awesome_imports.controller.success")
32
+ session[import_name.to_sym] = nil
33
+ redirect_to :action => :new
34
+ else
35
+ flash.now[:notice] = I18n.t("awesome_imports.controller.failed")
36
+ instance_variable_set("@#{import_name}", import)
37
+ render :create
38
+ end
39
+ end
40
+ end
41
+
42
+ module ClassMethods
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module AwesomeImports
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "awesome_imports/version"
2
+ require 'active_support'
3
+
4
+ module AwesomeImports
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :CsvImport
8
+ autoload :CsvImportsController
9
+ end
@@ -0,0 +1,61 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe AwesomeImports::CsvImport do
4
+ before do
5
+ @import = KeywordImport.new
6
+ end
7
+ describe "#persisted?" do
8
+ it "should success to call" do
9
+ @import.persisted?.should be_false
10
+ end
11
+ end
12
+
13
+ describe "#id" do
14
+ it "should success to call" do
15
+ @import.id.should be_nil
16
+ end
17
+ end
18
+
19
+ describe "#initialize" do
20
+ before do
21
+ @mock_csv = mock(:csv)
22
+ @import = KeywordImport.new(:csv_file => @mock_csv)
23
+ end
24
+ it "should success to call" do
25
+ @import.csv_file.should == @mock_csv
26
+ end
27
+ end
28
+
29
+ describe "#confirm" do
30
+ end
31
+
32
+ describe "#parse_csv_from_uploaded_file" do
33
+ end
34
+
35
+ describe "#parse_csv" do
36
+ end
37
+
38
+ describe "#records" do
39
+ it "should success to call" do
40
+ @import.records.should be_is_a(Array)
41
+ end
42
+ end
43
+
44
+ describe "#store_attached_csv_file" do
45
+ end
46
+
47
+ describe "#csv_tmp_dir" do
48
+ end
49
+
50
+ describe ".restore_from_file" do
51
+ end
52
+
53
+ describe "#load_stored_csv" do
54
+ end
55
+
56
+ describe "#parse_csv_from_stored_file" do
57
+ end
58
+
59
+ describe "#update" do
60
+ end
61
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe AwesomeImports::CsvImportsController do
4
+ describe "#index" do
5
+ before do
6
+ @controller = create_controller
7
+ end
8
+ it "should success to call" do
9
+ @controller.new
10
+ @controller.instance_variable_get('@keyword_import').should be_is_a(KeywordImport)
11
+ end
12
+ end
13
+
14
+ describe "#create" do
15
+ before do
16
+ @param = mock(:param)
17
+ @controller = create_controller({ :keyword_import => @param })
18
+
19
+ @import = mock(:import)
20
+ KeywordImport.stub(:new).and_return(@import)
21
+ @import.stub(:confirm)
22
+ @import.stub(:store_attached_csv_file)
23
+ end
24
+ it "should success to call" do
25
+ @controller.create
26
+ @controller.instance_variable_get('@keyword_import').should == @import
27
+ end
28
+ end
29
+
30
+ describe "#update" do
31
+ before do
32
+ @param = mock(:param)
33
+ @controller = create_controller({ :keyword_import => @param })
34
+
35
+ @import = mock(:import)
36
+ @import.stub(:update).and_return(true)
37
+ KeywordImport.stub(:restore_from_file).and_return(@import)
38
+ end
39
+ it "should success to call" do
40
+ @controller.should_receive(:redirect_to).with({ :action => :new })
41
+ @controller.update
42
+ end
43
+ end
44
+
45
+ def create_controller(params={}, session = {})
46
+ req = Struct.new(:parameters)
47
+ request = req.new(params)
48
+ controller = KeywordImportsController.new
49
+ controller.request = request
50
+ controller.stub(:session).and_return(session)
51
+ controller.stub(:flash).and_return({})
52
+ controller.stub(:redirect_to)
53
+ controller
54
+ end
55
+ end
data/spec/fake_app.rb ADDED
@@ -0,0 +1,6 @@
1
+ class KeywordImport < AwesomeImports::CsvImport
2
+ end
3
+
4
+ class KeywordImportsController < ActionController::Base
5
+ include AwesomeImports::CsvImportsController
6
+ end
@@ -0,0 +1,2 @@
1
+ 111,awesome,mat_aki
2
+ 222,csv,sonicgarden
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require "awesome_imports"
5
+ require "action_controller"
6
+
7
+ require File.join(File.dirname(__FILE__), 'fake_app')
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awesome_imports
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Akihiro Matsumura
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-21 00:00:00.000000000 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: &2155000740 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2155000740
26
+ - !ruby/object:Gem::Dependency
27
+ name: activemodel
28
+ requirement: &2155000220 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2155000220
37
+ - !ruby/object:Gem::Dependency
38
+ name: actionpack
39
+ requirement: &2154999740 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: 3.0.0
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *2154999740
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: &2154999260 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2154999260
59
+ - !ruby/object:Gem::Dependency
60
+ name: rspec
61
+ requirement: &2154998780 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *2154998780
70
+ description: Cool and simple data importer for rails by CSV
71
+ email:
72
+ - matsumura.aki@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.rdoc
81
+ - Rakefile
82
+ - awesome_imports.gemspec
83
+ - lib/awesome_imports.rb
84
+ - lib/awesome_imports/csv_import.rb
85
+ - lib/awesome_imports/csv_imports_controller.rb
86
+ - lib/awesome_imports/version.rb
87
+ - spec/csv_import_spec.rb
88
+ - spec/csv_imports_controller_spec.rb
89
+ - spec/fake_app.rb
90
+ - spec/fixtures/keywords.csv
91
+ - spec/spec_helper.rb
92
+ has_rdoc: true
93
+ homepage: https://github.com/mataki/awesome_imports
94
+ licenses: []
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project: awesome_imports
113
+ rubygems_version: 1.6.2
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Awesome csv importer for rails
117
+ test_files:
118
+ - spec/csv_import_spec.rb
119
+ - spec/csv_imports_controller_spec.rb
120
+ - spec/fake_app.rb
121
+ - spec/fixtures/keywords.csv
122
+ - spec/spec_helper.rb