Skip to content

Commit

Permalink
Removed file property from export model
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaguindani committed Jun 27, 2018
1 parent 8df0e1b commit 6cdfb7e
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 35 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
spec/examples.txt

# Ignore exports
/exports/*
!/exports/.keep


# Ignore RubyMine files
.idea
1 change: 0 additions & 1 deletion app/controllers/catalog_admin/exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def create
user: current_user,
catalog: catalog,
category: category,
file: false,
status: "processing"
)
redirect_to :back, :alert => @message
Expand Down
12 changes: 6 additions & 6 deletions app/models/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
#
# Table name: exports
#
# id :integer not null, primary key
# user_id :integer
# catalog_id :integer
# category :string
# status :string
# file :boolean
# created_at :datetime not null
# id :integer not null, primary key
# status :string
# updated_at :datetime not null
# user_id :integer
#

class Export < ActiveRecord::Base
Expand All @@ -30,7 +29,8 @@ class Export < ActiveRecord::Base
end

def pathname
Rails.root.join('exports').to_s + "/#{id}_#{catalog.slug}.zip"
ext = Rails.env.test? ? "test" : "zip"
Rails.root.join('exports').to_s + "/#{id}_#{catalog.slug}.#{ext}"
end

def validity?
Expand All @@ -42,7 +42,7 @@ def ready?
end

def file?
file
File.exist? pathname
end

def self.validity
Expand Down
8 changes: 4 additions & 4 deletions app/workers/export_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def perform(export_id, category)
when "catima"
catima_export(export, dir)
else
export.update(status: "error", file: false)
export.update(status: "error")
end

FileUtils.remove_entry dir
Expand All @@ -21,14 +21,14 @@ def perform(export_id, category)
private

def catima_export(export, dir)
params = { :status => "ready", :file => true }
status = "ready"
begin
CatalogDump.new.dump(export.catalog.slug, dir)
zip(dir, export.pathname)
rescue StandardError
params[:status] = "error", params[:file] = false
status = "error"
end
export.update(status: params[:status], file: params[:file])
export.update(status: status)
send_mail(export)
end

Expand Down
1 change: 0 additions & 1 deletion db/migrate/20180615090214_create_exports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def change
t.references :catalog, index: true, foreign_key: true
t.string :category
t.string :status
t.boolean :file

t.timestamps null: false
end
Expand Down
1 change: 0 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ CREATE TABLE exports (
catalog_id integer,
category character varying,
status character varying,
file boolean,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
Expand Down
Empty file removed exports/.keep
Empty file.
Binary file added exports/1_one.test
Binary file not shown.
Binary file added exports/2_two.test
Binary file not shown.
5 changes: 2 additions & 3 deletions lib/tasks/export.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ namespace :export do
desc "Check catalog exports validity and delete export files when expired"
task remove_expired: [:environment] do
# Check all the exports created more than 1 week ago
exports = Export.where("created_at < ?", Export.validity.ago).where(:file => true)
exports = Export.where("created_at < ?", Export.validity.ago)

# Remove all expired export files but keep the db records
exports.each do |export|
FileUtils.rm_f(export.pathname)
export.update(file: false)
FileUtils.rm_f(export.pathname) if export.file?
end
end
end
31 changes: 14 additions & 17 deletions test/fixtures/exports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,48 @@
#
# Table name: exports
#
# id :integer not null, primary key
# user_id :integer
# catalog_id :integer
# category :string
# file :boolean
# created_at :datetime not null
# id :integer not null, primary key
# status :string
# updated_at :datetime not null
# created_at :datetime not null
# user_id :integer
#

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
id: 1
user: one_admin
catalog: one
category: "catima"
status: "ready"
file: true

two:
id: 2
user: two_admin
catalog: two
category: "catima"
status: "ready"

one_expired:
id: 3
user: one_admin
catalog: one
category: "catima"
status: "ready"
file: false
created_at: <%= Time.zone.now - 8.days %>
updated_at: <%= Time.zone.now - 8.days %>

one_processing:
id: 4
user: one_admin
catalog: one
category: "catima"
status: "processing"
file: false

one_error:
id: 5
user: one_admin
catalog: one
category: "catima"
status: "error"
file: false

two:
user: two_admin
catalog: two
category: "catima"
status: "ready"
file: true

0 comments on commit 6cdfb7e

Please sign in to comment.