diff options
| author | Alban Peignier | 2012-04-30 17:14:37 +0200 | 
|---|---|---|
| committer | Alban Peignier | 2012-04-30 17:14:37 +0200 | 
| commit | a51e50beaefa809d2ed9fd9d7a8afe73e0cca3a8 (patch) | |
| tree | 1cec94c183ca905afb317e1acff3c70b0116c9d4 | |
| parent | cca4d6c96bae2bad367eb44e1ba278a02fbe62ae (diff) | |
| download | chouette-core-a51e50beaefa809d2ed9fd9d7a8afe73e0cca3a8.tar.bz2 | |
Prepare Referential by using Chouette::Loader import if a file is given. Refs #15
| -rw-r--r-- | Gemfile | 2 | ||||
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | app/models/referential.rb | 43 | ||||
| -rw-r--r-- | app/views/referentials/_form.erb | 5 | ||||
| -rw-r--r-- | config/environments/development.rb | 6 | ||||
| -rw-r--r-- | config/initializers/devise.rb | 2 | 
6 files changed, 53 insertions, 7 deletions
@@ -29,7 +29,7 @@ gem 'squeel'  gem "acts_as_tree", :git => "git://github.com/dryade/acts_as_tree.git"  gem 'apartment', :git => 'git://github.com/dryade/apartment.git' -gem 'ninoxe', :git => 'git://chouette.dryade.priv/ninoxe'#, :path => '~/projects/ninoxe' +gem 'ninoxe', :git => 'git://chouette.dryade.priv/ninoxe' #, :path => '~/Projects/Ninoxe'  # Gems used only for assets and not required  # in production environments by default. diff --git a/Gemfile.lock b/Gemfile.lock index c22b08942..bdbcf1e80 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@  GIT    remote: git://chouette.dryade.priv/ninoxe -  revision: 8b4136d1c06cc03c22443cae4408b5b173dca9b5 +  revision: 1b155a933eeea7b1a744ef452da81c4e2d05aba3    specs:      ninoxe (0.0.8)        GeoRuby diff --git a/app/models/referential.rb b/app/models/referential.rb index a05e3f065..dd5908b8b 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -2,9 +2,6 @@ class Referential < ActiveRecord::Base    validates_presence_of :name     validates_presence_of :slug -  before_create :create_schema  -  before_destroy :destroy_schema -    def lines      Chouette::Line.scoped    end @@ -31,7 +28,45 @@ class Referential < ActiveRecord::Base      self    end -  private  +  before_create :prepare +  before_destroy :destroy_schema + +  attr_accessor :resources +  attr_accessor :loader + +  def loader +    @loader ||= ::Chouette::Loader.new(slug) +  end + +  def prepare +    if resources +      import_resources +    else +      create_schema +    end +  end + +  def with_original_filename +    Dir.mktmpdir do |tmp_dir| +      tmp_link = File.join(tmp_dir, resources.original_filename) +      FileUtils.ln_s resources.path, tmp_link +      yield tmp_link +    end +  end + +  def import_resources +    # Apartment::Database.create create tables +    loader.create +    begin +      with_original_filename do |file| +        # chouette-command checks the file extension (and requires .zip) :( +        loader.import file +      end +    rescue => e +      loader.drop +      raise e +    end +  end    def create_schema      Apartment::Database.create slug diff --git a/app/views/referentials/_form.erb b/app/views/referentials/_form.erb index 031621b00..34d515c4a 100644 --- a/app/views/referentials/_form.erb +++ b/app/views/referentials/_form.erb @@ -2,8 +2,13 @@    <%= form.inputs do %>       <%= form.input :name %>       <%= form.input :slug%> + +    <% if @referential.new_record? %> +    <%= form.input :resources, :as => :file %> +    <% end %>    <% end %> +    <%= form.buttons do %>      <%= form.commit_button true %>      <li><%= t('or') %></li> diff --git a/config/environments/development.rb b/config/environments/development.rb index ebf0171fc..6c39bf8eb 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -29,4 +29,10 @@ ChouetteIhm::Application.configure do    config.assets.debug = true    config.action_mailer.default_url_options = { :host => 'localhost:3000' } + +  config.to_prepare do +    # Chouette::Loader.chouette_command = "true" +    Chouette::Loader.chouette_command = "tmp/chouette-command/chouette" +  end +    end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index e66681d03..6b256c3b4 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -55,7 +55,7 @@ Devise.setup do |config|    # It can be set to an array that will enable http authentication only for the    # given strategies, for example, `config.http_authenticatable = [:token]` will    # enable it only for token authentication. -  # config.http_authenticatable = false +  config.http_authenticatable = true    # If http headers should be returned for AJAX requests. True by default.    # config.http_authenticatable_on_xhr = true  | 
