Skip to content

Commit

Permalink
Merge pull request #2 from luilver/xls_files
Browse files Browse the repository at this point in the history
Xls files
  • Loading branch information
luilver authored Dec 16, 2020
2 parents 4e72e0c + 06f29f0 commit 99e209b
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ RUN apt-get update && \
apt-get autoremove -y && \
cp config/database.postgres.docker.yml config/database.yml && \
gem install bundler && \
bundle install --deployment && \
bundle config set deployment 'true' && \
bundle install && \
bundle exec rails assets:precompile

CMD ["bundle","exec","rails","s"]
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ gem "devise-encryptable"
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
gem 'activejob', '~> 5.2.0'
gem 'ransack_ui', path: 'vendor/gems/ransack_ui-1.3.4' # Vendored until our fix is merged and released
gem 'spreadsheet'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ GEM
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.7)
ruby-ole (1.2.12.2)
ruby-progressbar (1.10.1)
rubyzip (2.3.0)
sass (3.7.4)
Expand All @@ -359,6 +360,8 @@ GEM
sixarm_ruby_unaccent (1.2.0)
sort_alphabetical (1.1.0)
unicode_utils (>= 1.2.2)
spreadsheet (1.2.6)
ruby-ole (>= 1.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -470,6 +473,7 @@ DEPENDENCIES
select2-rails
selenium-webdriver
simple_form
spreadsheet
sprockets-rails (>= 3.0.0)
sqlite3 (~> 1.3.13)
therubyracer
Expand Down
43 changes: 43 additions & 0 deletions app/models/files/imported_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
# == Schema Information
#
# Table name: imported_files
#
# id :integer not null, primary key
# filename :string(64) default(""), not null
# md5sum :string(32) default(""), not null
#

class ImportedFile < ActiveRecord::Base
before_validation :generate_md5sum

validate :filetype

validates :filename, presence: true
validates :md5sum, presence: true
validates :md5sum, uniqueness: { message: "file already imported" }

def generate_md5sum
self.md5sum = Digest::MD5.hexdigest File.open(filename).read unless filename.empty? rescue ""
end

private

def filetype
valid = File.open(filename).type_from_file_command == "application/vnd.ms-excel" rescue ""
if valid == ""
errors.add(:filename, "no such file")
end
unless valid
errors.add(:filename, "invalid filetype")
end
end

ActiveSupport.run_load_hooks(:fat_free_crm_imported_file, self)
end
15 changes: 15 additions & 0 deletions config/initializers/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
File.class_eval do
def type_from_file_command
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
mime_type = `file -b --mime-type #{self.path}`.split(':').last.strip rescue "application/x-#{type}"
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
mime_type
end
end
16 changes: 16 additions & 0 deletions db/migrate/20201103150431_create_imported_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class CreateImportedFiles < ActiveRecord::Migration[4.2]
def self.up
create_table :imported_files, force: true do |t|
t.string :filename, limit: 64, null: false, default: ""
t.string :md5sum, limit: 32, null: false, default: ""

t.timestamps
end
end

def self.down
drop_table :imported_files
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_01_07_082701) do
ActiveRecord::Schema.define(version: 2020_11_03_150431) do

create_table "account_contacts", force: :cascade do |t|
t.integer "account_id"
Expand Down Expand Up @@ -241,6 +241,13 @@
t.index ["user_id"], name: "index_groups_users_on_user_id"
end

create_table "imported_files", force: :cascade do |t|
t.string "filename", limit: 64, default: "", null: false
t.string "md5sum", limit: 32, default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "leads", force: :cascade do |t|
t.integer "user_id"
t.integer "campaign_id"
Expand Down
13 changes: 13 additions & 0 deletions spec/factories/imported_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
FactoryBot.define do
factory :imported_file do
filename { "MyString" }
md5sum { "MyString" }
end
end
21 changes: 21 additions & 0 deletions spec/models/files/imported_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
# == Schema Information
#
# Table name: imported_files
#
# id :integer not null, primary key
# filename :string(64) default(""), not null
# md5sum :string(32) default(""), not null
#

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

RSpec.describe ImportedFile, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 99e209b

Please sign in to comment.