diff options
| author | Xinhui | 2016-12-27 11:52:43 +0100 | 
|---|---|---|
| committer | Xinhui | 2016-12-27 12:11:49 +0100 | 
| commit | f8c53054f2243dd53c1c34a810657be60a59c2e6 (patch) | |
| tree | 9696fe6cbd6f4f2bc7fb2b80e463be15466e022f | |
| parent | 9cf671b341fce2416e7f1bc6d620a8fb86e5a211 (diff) | |
| download | chouette-core-f8c53054f2243dd53c1c34a810657be60a59c2e6.tar.bz2 | |
Model import
Refs #2261
| -rw-r--r-- | app/models/import.rb | 90 | ||||
| -rw-r--r-- | app/uploaders/import_uploader.rb | 49 | ||||
| -rw-r--r-- | config/routes.rb | 17 | ||||
| -rw-r--r-- | db/migrate/20120515134710_create_imports.rb | 11 | ||||
| -rw-r--r-- | db/migrate/20161227104202_create_imports.rb | 14 | ||||
| -rw-r--r-- | db/migrate/20161227104741_add_file_to_imports.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 17 | ||||
| -rw-r--r-- | spec/factories/imports.rb | 11 | ||||
| -rw-r--r-- | spec/models/import_spec.rb | 7 | 
9 files changed, 116 insertions, 105 deletions
| diff --git a/app/models/import.rb b/app/models/import.rb index 078f1698e..8ffac5c5e 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -1,87 +1,7 @@ -require 'open-uri' +class Import < ActiveRecord::Base +  mount_uploader :file, ImportUploader +  belongs_to :workbench +  belongs_to :referential -class Import -  include JobConcern - -  def initialize( response ) -    @datas = response -  end - -  def report? -    links["action_report"].present? -  end - -  def report -    Rails.cache.fetch("#{cache_key}/action_report", expires_in: cache_expiration) do -      report_path = links["action_report"] -      if report_path -        response = Ievkit.get(report_path) -        ImportReport.new(response) -      else -        nil -      end -    end -  end - -  def rule_parameter_set? -    links["validation_params"].present? -  end - -  def rule_parameter_set -    Rails.cache.fetch("#{cache_key}/validation_params", expires_in: cache_expiration) do -      rule_parameter_set_path = links["validation_params"] -      if rule_parameter_set_path -        response = Ievkit.get(rule_parameter_set_path) -        rule_parameter_set = RuleParameterSet.new(:name => "", :import => self).tap { |rps| rps.parameters = response.validation } -      else -        nil -      end -    end -  end - -  def compliance_check? -    links["validation_report"].present? -  end - -  def compliance_check_validation_report -    puts "compliance_check_validation_report" -    Rails.cache.fetch("#{cache_key}/validation_report", expires_in: cache_expiration) do -      compliance_check_path = links["validation_report"] -      if compliance_check_path -        response = Ievkit.get(compliance_check_path) -        ComplianceCheckResult.new(response) -      else -        nil -      end -    end -  end - -  def destroy -    delete_path =  links["delete"] -    cancel_path = links["cancel"] - -    if delete_path -      Ievkit.delete(delete_path) -    elsif cancel_path -      Ievkit.delete(cancel_path) -    else -      nil -    end -  end - -  def file_path? -    links["data"].present? -  end - -  def file_path -    links["data"] -  end - -  def filename -    File.basename(file_path) if file_path -  end - -  def filename_extension -    File.extname(filename).gsub(".", "") if filename -  end +  validates :file, presence: true  end diff --git a/app/uploaders/import_uploader.rb b/app/uploaders/import_uploader.rb new file mode 100644 index 000000000..2740393ca --- /dev/null +++ b/app/uploaders/import_uploader.rb @@ -0,0 +1,49 @@ +class ImportUploader < CarrierWave::Uploader::Base + +  # Include RMagick or MiniMagick support: +  # include CarrierWave::RMagick +  # include CarrierWave::MiniMagick + +  # Choose what kind of storage to use for this uploader: +  storage :file +  # storage :fog + +  # Override the directory where uploaded files will be stored. +  # This is a sensible default for uploaders that are meant to be mounted: +  def store_dir +    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" +  end + +  # Provide a default URL as a default if there hasn't been a file uploaded: +  # def default_url +  #   # For Rails 3.1+ asset pipeline compatibility: +  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) +  # +  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_') +  # end + +  # Process files as they are uploaded: +  # process scale: [200, 300] +  # +  # def scale(width, height) +  #   # do something +  # end + +  # Create different versions of your uploaded files: +  # version :thumb do +  #   process resize_to_fit: [50, 50] +  # end + +  # Add a white list of extensions which are allowed to be uploaded. +  # For images you might use something like this: +  # def extension_whitelist +  #   %w(jpg jpeg gif png) +  # end + +  # Override the filename of the uploaded files: +  # Avoid using model.id or version_name here, see uploader/store.rb for details. +  # def filename +  #   "something.jpg" if original_filename +  # end + +end diff --git a/config/routes.rb b/config/routes.rb index c49b28a07..30aea2295 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -119,14 +119,15 @@ ChouetteIhm::Application.routes.draw do      end      resources :import_tasks, :only => [:new, :create] -    resources :imports, :only => [:index, :show, :destroy] do -      member do -        get "imported_file" -        get "rule_parameter_set" -        get "compliance_check" -        get 'export', defaults: { format: 'zip' } -      end -    end +    resources :imports +    # fixme #2261 +      # member do +      #   get "imported_file" +      #   get "rule_parameter_set" +      #   get "compliance_check" +      #   get 'export', defaults: { format: 'zip' } +      # end +    # end      resources :export_tasks, :only => [:new, :create] do        collection do diff --git a/db/migrate/20120515134710_create_imports.rb b/db/migrate/20120515134710_create_imports.rb deleted file mode 100644 index 5da398fc9..000000000 --- a/db/migrate/20120515134710_create_imports.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CreateImports < ActiveRecord::Migration -  def change -    create_table :imports do |t| -      t.belongs_to :referential -      t.string :status - -      t.timestamps -    end -    add_index :imports, :referential_id -  end -end diff --git a/db/migrate/20161227104202_create_imports.rb b/db/migrate/20161227104202_create_imports.rb new file mode 100644 index 000000000..97a2d6029 --- /dev/null +++ b/db/migrate/20161227104202_create_imports.rb @@ -0,0 +1,14 @@ +class CreateImports < ActiveRecord::Migration +  def change +    create_table :imports do |t| +      t.string :status +      t.string :current_step_id +      t.float :current_step_progress +      t.references :workbench, index: true +      t.references :referential, index: true +      t.string :name + +      t.timestamps +    end +  end +end diff --git a/db/migrate/20161227104741_add_file_to_imports.rb b/db/migrate/20161227104741_add_file_to_imports.rb new file mode 100644 index 000000000..36498039b --- /dev/null +++ b/db/migrate/20161227104741_add_file_to_imports.rb @@ -0,0 +1,5 @@ +class AddFileToImports < ActiveRecord::Migration +  def change +    add_column :imports, :file, :string +  end +end diff --git a/db/schema.rb b/db/schema.rb index 0277cae91..734dde1de 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@  #  # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161208120132) do +ActiveRecord::Schema.define(version: 20161227104741) do    # These are extensions that must be enabled in order to support this database    enable_extension "plpgsql" @@ -224,6 +224,21 @@ ActiveRecord::Schema.define(version: 20161208120132) do      t.integer "line_id",          limit: 8    end +  create_table "imports", force: true do |t| +    t.string   "status" +    t.string   "current_step_id" +    t.float    "current_step_progress" +    t.integer  "workbench_id" +    t.integer  "referential_id" +    t.string   "name" +    t.datetime "created_at" +    t.datetime "updated_at" +    t.string   "file" +  end + +  add_index "imports", ["referential_id"], :name => "index_imports_on_referential_id" +  add_index "imports", ["workbench_id"], :name => "index_imports_on_workbench_id" +    create_table "journey_frequencies", force: true do |t|      t.integer  "vehicle_journey_id",         limit: 8      t.time     "scheduled_headway_interval",                           null: false diff --git a/spec/factories/imports.rb b/spec/factories/imports.rb new file mode 100644 index 000000000..8b2cb223d --- /dev/null +++ b/spec/factories/imports.rb @@ -0,0 +1,11 @@ +FactoryGirl.define do +  factory :import do +    status "MyString" +current_step_id "MyString" +current_step_progress 1.5 +workbench nil +referential nil +name "MyString" +  end + +end diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb new file mode 100644 index 000000000..10a508c71 --- /dev/null +++ b/spec/models/import_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe Import, :type => :model do +  it { should belong_to(:referential) } +  it { should belong_to(:workbench) } +  it { should validate_presence_of(:file) } +end | 
