diff options
| author | Alban Peignier | 2012-07-09 17:53:44 +0200 |
|---|---|---|
| committer | Alban Peignier | 2012-07-09 17:53:44 +0200 |
| commit | b7440a9796794b411acc3d8e47f357525099a376 (patch) | |
| tree | 39b16430271b1659fbb5d5a39ebfd1c1c902861a | |
| parent | 5fcb7b6e88ce6c243f80ac92baca5c33704ab63a (diff) | |
| download | chouette-core-b7440a9796794b411acc3d8e47f357525099a376.tar.bz2 | |
Add first support for Cvs and GTFS exports. Refs #66. Refs #67
| -rw-r--r-- | Gemfile | 1 | ||||
| -rw-r--r-- | Gemfile.lock | 1 | ||||
| -rw-r--r-- | app/assets/javascripts/exports.js.coffee | 19 | ||||
| -rw-r--r-- | app/assets/javascripts/imports.js.coffee | 4 | ||||
| -rw-r--r-- | app/assets/stylesheets/exports.css.scss | 15 | ||||
| -rw-r--r-- | app/controllers/exports_controller.rb | 17 | ||||
| -rw-r--r-- | app/helpers/exports_helper.rb | 7 | ||||
| -rw-r--r-- | app/models/csv_export.rb | 7 | ||||
| -rw-r--r-- | app/models/export.rb | 26 | ||||
| -rw-r--r-- | app/models/gtfs_export.rb | 7 | ||||
| -rw-r--r-- | app/views/exports/new.html.erb | 12 | ||||
| -rw-r--r-- | app/views/exports/new.js.coffee | 2 | ||||
| -rw-r--r-- | spec/models/csv_export_spec.rb | 7 | ||||
| -rw-r--r-- | spec/models/export_spec.rb | 3 | ||||
| -rw-r--r-- | spec/models/gtfs_export_spec.rb | 7 | ||||
| -rw-r--r-- | spec/models/neptune_export_spec.rb | 7 |
16 files changed, 118 insertions, 24 deletions
@@ -68,6 +68,7 @@ group :test, :development do gem "remarkable", "~> 4.0.0.alpha4" gem "remarkable_activerecord", "~> 4.0.0.alpha4" gem "shoulda-matchers" + gem 'rb-inotify', :platform => :ruby end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 334ded324..fe315519f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -371,6 +371,7 @@ DEPENDENCIES pg (~> 0.11.0) rails (= 3.2.6) ransack + rb-inotify remarkable (~> 4.0.0.alpha4) remarkable_activerecord (~> 4.0.0.alpha4) rspec diff --git a/app/assets/javascripts/exports.js.coffee b/app/assets/javascripts/exports.js.coffee index 025f9fcce..6de0d4692 100644 --- a/app/assets/javascripts/exports.js.coffee +++ b/app/assets/javascripts/exports.js.coffee @@ -1,18 +1,23 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ - jQuery -> export_references_type_change = (event) -> - references_type = $("select option:selected").attr("value") + references_type = $(event.target).val() toggle_input = (li) -> + console.log(li) enabled = (li.data("type") == references_type) # Hide li block li.toggle(enabled) # Disable textarea to ignore it in POST data li.find("textarea").attr("disabled", ! enabled) - toggle_input($(li)) for li in $(".export_reference_ids") + toggle_input($(li)) for li in $(event.target).parents('form').find("li.export_reference_ids") + + $('select[name="export[references_type]"]').change(export_references_type_change) + + $('#export_type_submit').hide() + + export_type_change = (event) -> + export_type = $("select option:selected").attr("value") + $(form).toggle($(form).is("#" + export_type + "_new")) for form in $('form.export') - $('#export_references_type').change(export_references_type_change) + $('#export_type').change(export_type_change) diff --git a/app/assets/javascripts/imports.js.coffee b/app/assets/javascripts/imports.js.coffee index 7fb49e44d..ba7372e13 100644 --- a/app/assets/javascripts/imports.js.coffee +++ b/app/assets/javascripts/imports.js.coffee @@ -1,7 +1,3 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ - jQuery -> $('#import_type_submit').hide() diff --git a/app/assets/stylesheets/exports.css.scss b/app/assets/stylesheets/exports.css.scss index cb8f23dc4..0af4af23a 100644 --- a/app/assets/stylesheets/exports.css.scss +++ b/app/assets/stylesheets/exports.css.scss @@ -39,3 +39,18 @@ } } } + +#workspace.exports.new form.new_export { + padding: 0.5em 0; + margin-top: -0.5em; + margin-bottom: 1em; + + label { + display: block; + width: 25%; + float: left; + } + select { + width: 25%; + } +}
\ No newline at end of file diff --git a/app/controllers/exports_controller.rb b/app/controllers/exports_controller.rb index ee7e186b9..3b8fee566 100644 --- a/app/controllers/exports_controller.rb +++ b/app/controllers/exports_controller.rb @@ -5,8 +5,15 @@ class ExportsController < ChouetteController belongs_to :referential + def new + new! do + available_exports + end + end + def create create! do |success, failure| + available_exports success.html { redirect_to referential_exports_path(@referential) } end end @@ -28,6 +35,16 @@ class ExportsController < ChouetteController protected + def available_exports + @available_exports ||= Export.types.collect do |type| + unless @export.type == type + @referential.exports.build :type => type + else + @export + end + end + end + # FIXME why #resource_id is nil ?? def build_resource super.tap do |export| diff --git a/app/helpers/exports_helper.rb b/app/helpers/exports_helper.rb index 1b441f995..55bbafb72 100644 --- a/app/helpers/exports_helper.rb +++ b/app/helpers/exports_helper.rb @@ -21,6 +21,10 @@ module ExportsHelper "export_reference_#{relation_name}_ids" end + def input_class + "export_#{relation_name}_reference_ids" + end + end def export_references_input(form, export, type) @@ -34,7 +38,7 @@ module ExportsHelper @form, @export, @type_helper = form, export, type_helper end - delegate :input_id, :type, :to => :type_helper + delegate :input_id, :type, :input_class, :to => :type_helper def current? export.references_type == type @@ -56,6 +60,7 @@ module ExportsHelper def input_html_options { :id => input_id, + :class => input_class, :"data-pre" => ( references_map.to_json if current? ), :disabled => (true unless current?) } diff --git a/app/models/csv_export.rb b/app/models/csv_export.rb new file mode 100644 index 000000000..4afacf81a --- /dev/null +++ b/app/models/csv_export.rb @@ -0,0 +1,7 @@ +class CsvExport < Export + + def export_options + super.merge(:format => :csv) + end + +end diff --git a/app/models/export.rb b/app/models/export.rb index a83d89958..d3a73b1ff 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -84,14 +84,6 @@ class Export < ActiveRecord::Base log_messages.create :key => status end - def self.new(attributes = {}, options = {}, &block) - if self == Export - Object.const_get(attributes.delete(:type) || "NeptuneExport").new(attributes, options) - else - super - end - end - @@references_types = [ Chouette::Line, Chouette::Network, Chouette::Company ] cattr_reader :references_types @@ -143,4 +135,22 @@ class Export < ActiveRecord::Base read_attribute :reference_ids end + def self.types + unless Rails.env.development? + subclasses.map(&:to_s) + else + # FIXME + %w{NeptuneExport CsvExport GtfsExport} + end + end + + def self.new(attributes = {}, options = {}, &block) + if self == Export + Object.const_get(attributes.delete(:type) || "NeptuneExport").new(attributes, options) + else + super + end + end + end + diff --git a/app/models/gtfs_export.rb b/app/models/gtfs_export.rb new file mode 100644 index 000000000..8edad4b1f --- /dev/null +++ b/app/models/gtfs_export.rb @@ -0,0 +1,7 @@ +class GtfsExport < Export + + def export_options + super.merge(:format => :gtfs) + end + +end diff --git a/app/views/exports/new.html.erb b/app/views/exports/new.html.erb index 82409955b..36a37abc4 100644 --- a/app/views/exports/new.html.erb +++ b/app/views/exports/new.html.erb @@ -1,7 +1,16 @@ <%= title_tag t(".title") %> -<%= semantic_form_for [@referential, @export], :as => :export, :url => referential_exports_path(@referential) do |form| %> +<%= form_for([@referential, @export], :as => :export, :url => new_referential_export_path(@referential), :method => :get) do |form| %> + <%= form.label :type %> + <%= form.collection_select :type, Export.types, :to_s, :to_s %> + <%= form.submit "Sélectionner", :id => "export_type_submit" %> +<% end %> + +<% @available_exports.each do |export| %> +<%= semantic_form_for [@referential, export], :as => :export, :url => referential_exports_path(@referential), :html => { :id => "#{export.type}_new", :style => ('display: none' unless @export == export)} do |form| %> <%= form.inputs do %> + <%= form.input :type, :as => :hidden %> + <%= form.input :references_type, :as => :select, :collection => Export.references_types.map { |c| [ c.model_name.human.capitalize.pluralize, c.name ] }, :include_blank => t(".all") %> <% Export.references_types.each do |type| %> @@ -14,5 +23,6 @@ <%= form.action :cancel, :as => :link %> <% end %> <% end %> +<% end %> <%= javascript_include_tag new_referential_export_path(@referential, :format => :js) %> diff --git a/app/views/exports/new.js.coffee b/app/views/exports/new.js.coffee index 198af96c8..4a1d59f92 100644 --- a/app/views/exports/new.js.coffee +++ b/app/views/exports/new.js.coffee @@ -1,4 +1,4 @@ jQuery -> <% Export.references_types.each do |type| %> - $("#<%= export_references_type(type).input_id %>" ).tokenInput('<%= references_referential_exports_path(@referential, :type => export_references_type(type).relation_name, :format => :json) %>', { prePopulate: $('#').data('pre'), minChars: 3 }); + $("textarea.<%= export_references_type(type).input_class %>").tokenInput('<%= references_referential_exports_path(@referential, :type => export_references_type(type).relation_name, :format => :json) %>', { prePopulate: $('#').data('pre'), minChars: 3 }); <% end %> diff --git a/spec/models/csv_export_spec.rb b/spec/models/csv_export_spec.rb new file mode 100644 index 000000000..8c80ab8da --- /dev/null +++ b/spec/models/csv_export_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe CsvExport do + + its(:export_options) { should include(:format => :csv) } + +end diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb index 4e6a748ed..c59689f4a 100644 --- a/spec/models/export_spec.rb +++ b/spec/models/export_spec.rb @@ -46,8 +46,7 @@ describe Export do describe ".types" do it "should return available Export implementations" do - pending - Export.types.should =~ %w{NeptuneExport} + Export.types.should =~ %w{NeptuneExport CsvExport GtfsExport} end end diff --git a/spec/models/gtfs_export_spec.rb b/spec/models/gtfs_export_spec.rb new file mode 100644 index 000000000..4bc587f14 --- /dev/null +++ b/spec/models/gtfs_export_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe GtfsExport do + + its(:export_options) { should include(:format => :gtfs) } + +end diff --git a/spec/models/neptune_export_spec.rb b/spec/models/neptune_export_spec.rb new file mode 100644 index 000000000..1b4f4afa9 --- /dev/null +++ b/spec/models/neptune_export_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe NeptuneExport do + + its(:export_options) { should include(:format => :neptune) } + +end |
