aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/assets/javascripts/exports.js.coffee15
-rw-r--r--app/assets/stylesheets/token-input.css3
-rw-r--r--app/controllers/exports_controller.rb10
-rw-r--r--app/controllers/stop_area_children_controller.rb2
-rw-r--r--app/helpers/exports_helper.rb68
-rw-r--r--app/models/export.rb64
-rw-r--r--app/views/exports/new.html.erb10
-rw-r--r--app/views/exports/new.js.coffee4
-rw-r--r--app/views/exports/show.html.erb11
-rw-r--r--config/locales/exports.yml6
-rw-r--r--config/routes.rb6
-rw-r--r--db/migrate/20120620064014_add_references_to_export.rb7
-rw-r--r--db/schema.rb5
-rw-r--r--spec/models/export_spec.rb88
14 files changed, 294 insertions, 5 deletions
diff --git a/app/assets/javascripts/exports.js.coffee b/app/assets/javascripts/exports.js.coffee
index 761567942..025f9fcce 100644
--- a/app/assets/javascripts/exports.js.coffee
+++ b/app/assets/javascripts/exports.js.coffee
@@ -1,3 +1,18 @@
# 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")
+
+ toggle_input = (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")
+
+ $('#export_references_type').change(export_references_type_change)
diff --git a/app/assets/stylesheets/token-input.css b/app/assets/stylesheets/token-input.css
index 03bb01c4d..55a7f5656 100644
--- a/app/assets/stylesheets/token-input.css
+++ b/app/assets/stylesheets/token-input.css
@@ -111,3 +111,6 @@ div.token-input-dropdown ul li.token-input-selected-dropdown-item {
background-color: #d0efa0;
}
+form.formtastic .token-input-list {
+display: inline-block;
+} \ No newline at end of file
diff --git a/app/controllers/exports_controller.rb b/app/controllers/exports_controller.rb
index 6a77f7f45..ee7e186b9 100644
--- a/app/controllers/exports_controller.rb
+++ b/app/controllers/exports_controller.rb
@@ -1,6 +1,6 @@
class ExportsController < ChouetteController
- respond_to :html, :xml, :json
+ respond_to :html, :xml, :json, :js
respond_to :zip, :only => :show
belongs_to :referential
@@ -17,6 +17,14 @@ class ExportsController < ChouetteController
end
end
+ def references
+ @references = referential.send(params[:type]).where("name ilike ?", "%#{params[:q]}%")
+ respond_to do |format|
+ format.json do
+ render :json => @references.collect { |child| { :id => child.id, :name => child.name } }
+ end
+ end
+ end
protected
diff --git a/app/controllers/stop_area_children_controller.rb b/app/controllers/stop_area_children_controller.rb
index a3a59047f..111fcb93c 100644
--- a/app/controllers/stop_area_children_controller.rb
+++ b/app/controllers/stop_area_children_controller.rb
@@ -8,6 +8,8 @@ class StopAreaChildrenController < ChouetteController
end
end
+ protected
+
def children_maps
children.collect do |child|
{ :id => child.id.to_s, :name => "#{child.name} #{child.country_code}" }
diff --git a/app/helpers/exports_helper.rb b/app/helpers/exports_helper.rb
index bd84b9886..1b441f995 100644
--- a/app/helpers/exports_helper.rb
+++ b/app/helpers/exports_helper.rb
@@ -1,2 +1,70 @@
module ExportsHelper
+
+ @@export_references_type = {}
+ def export_references_type(type)
+ @@export_references_type[type] ||= ReferencesTypeHelper.new(type)
+ end
+
+ class ReferencesTypeHelper
+
+ attr_accessor :type
+
+ def initialize(type)
+ @type = type.to_s
+ end
+
+ def relation_name
+ Export.references_relation(type)
+ end
+
+ def input_id
+ "export_reference_#{relation_name}_ids"
+ end
+
+ end
+
+ def export_references_input(form, export, type)
+ ReferencesInput.new form, export, export_references_type(type)
+ end
+
+ class ReferencesInput
+
+ attr_accessor :form, :export, :type_helper
+ def initialize(form, export, type_helper)
+ @form, @export, @type_helper = form, export, type_helper
+ end
+
+ delegate :input_id, :type, :to => :type_helper
+
+ def current?
+ export.references_type == type
+ end
+
+ def references_map
+ references.collect { |child| { :id => child.id, :name => child.name } }
+ end
+
+ def wrapper_html_options
+ {
+ :class => "export_reference_ids",
+ :id => "#{input_id}_input",
+ :"data-type" => type,
+ :style => ("display: none" unless current?)
+ }
+ end
+
+ def input_html_options
+ {
+ :id => input_id,
+ :"data-pre" => ( references_map.to_json if current? ),
+ :disabled => (true unless current?)
+ }
+ end
+
+ def input
+ form.input :reference_ids, :as => :text, :wrapper_html => wrapper_html_options, :input_html => input_html_options
+ end
+
+ end
+
end
diff --git a/app/models/export.rb b/app/models/export.rb
index 4fd6ff2f1..401541a13 100644
--- a/app/models/export.rb
+++ b/app/models/export.rb
@@ -46,7 +46,18 @@ class Export < ActiveRecord::Base
end
def export_options
- { :export_id => self.id }
+ { :export_id => self.id, :o => export_object_type }.tap do |options|
+ options[:id] = reference_ids.join(',') if reference_ids.present?
+ end
+ end
+
+ def export_object_type
+ case references_type
+ when "Chouette::Network"
+ "ptnetwork"
+ else
+ references_relation ? references_relation.singularize : "line"
+ end
end
before_validation :define_default_attributes, :on => :create
@@ -84,4 +95,55 @@ class Export < ActiveRecord::Base
end
end
+ @@references_types = [ Chouette::Line, Chouette::Network, Chouette::Company ]
+ cattr_reader :references_types
+
+ validates_inclusion_of :references_type, :in => references_types.map(&:to_s), :all_blank => true, :allow_nil => true
+
+ def references
+ if references_relation.present? and reference_ids.present?
+ referential.send(references_relation).find(reference_ids)
+ else
+ []
+ end
+ end
+
+ def references=(references)
+ unless references.blank?
+ self.references_type = references.first.class.name
+ self.reference_ids = references.map(&:id)
+ else
+ self.references_type = nil
+ self.reference_ids = []
+ end
+ end
+
+ def references_relation
+ if references_type.present?
+ relation = self.class.references_relation(references_type)
+ relation if referential.respond_to?(relation)
+ end
+ end
+
+ def self.references_relation(type)
+ type.to_s.demodulize.underscore.pluralize
+ end
+
+ def reference_ids
+ if raw_reference_ids
+ raw_reference_ids.split(',').map(&:to_i)
+ else
+ []
+ end
+ end
+
+ def reference_ids=(records)
+ records = Array(records)
+ write_attribute :reference_ids, (records.present? ? records.join(',') : nil)
+ end
+
+ def raw_reference_ids
+ read_attribute :reference_ids
+ end
+
end
diff --git a/app/views/exports/new.html.erb b/app/views/exports/new.html.erb
index 24022e0f8..16c6e9720 100644
--- a/app/views/exports/new.html.erb
+++ b/app/views/exports/new.html.erb
@@ -1,9 +1,19 @@
<%= title_tag t(".title") %>
<%= semantic_form_for [@referential, @export], :as => :export, :url => referential_exports_path(@referential) do |form| %>
+ <%= form.inputs do %>
+ <%= 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| %>
+ <%= export_references_input(form, @export, type).input %>
+ <% end %>
+ <% end %>
+
<%= form.buttons do %>
<%= form.commit_button true %>
<li><%= t('or') %></li>
<li><%= link_to t('cancel'), :back %></li>
<% 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
new file mode 100644
index 000000000..198af96c8
--- /dev/null
+++ b/app/views/exports/new.js.coffee
@@ -0,0 +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 });
+ <% end %>
diff --git a/app/views/exports/show.html.erb b/app/views/exports/show.html.erb
index c3248b47a..21271c302 100644
--- a/app/views/exports/show.html.erb
+++ b/app/views/exports/show.html.erb
@@ -12,6 +12,16 @@
</p>
</div>
+ <% if @export.references.present? %>
+ <h3><%= Export.human_attribute_name(:references) %></h3>
+ <ul>
+ <% @export.references.each do |reference| %>
+ <li><%= link_to "#{reference.class.model_name.humanize} #{reference.name}", [@referential, reference] %>
+ <% end %>
+ </ul>
+ <% end %>
+
+ <% if @export.log_messages.present? %>
<h3><%= t(".report") %></h3>
<table>
<tr>
@@ -29,6 +39,7 @@
</tr>
<% end %>
</table>
+ <% end %>
</div>
<% content_for :sidebar do %>
diff --git a/config/locales/exports.yml b/config/locales/exports.yml
index 19beced06..bf943c8af 100644
--- a/config/locales/exports.yml
+++ b/config/locales/exports.yml
@@ -7,6 +7,7 @@ en:
download: Download
new:
title: New export
+ all: All
index:
title: Exports
show:
@@ -38,6 +39,8 @@ en:
attributes:
export:
status: Status
+ references_type: Associated Data Type
+ reference_ids: Associated Data
export_log_message:
created_at: Date
position: N.
@@ -51,6 +54,7 @@ fr:
download: Télécharger
new:
title: Nouvel export
+ all: Toutes
index:
title: Exports
show:
@@ -86,6 +90,8 @@ fr:
attributes:
export:
status: Status
+ references_type: Type de données incluses
+ reference_ids: Données incluses
export_log_message:
created_at: Date
position: "No"
diff --git a/config/routes.rb b/config/routes.rb
index 313c52106..620c1d879 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -46,7 +46,11 @@ ChouetteIhm::Application.routes.draw do
end
resources :imports
- resources :exports
+ resources :exports do
+ collection do
+ get 'references'
+ end
+ end
resources :companies, :stop_areas
diff --git a/db/migrate/20120620064014_add_references_to_export.rb b/db/migrate/20120620064014_add_references_to_export.rb
new file mode 100644
index 000000000..c993c1c5d
--- /dev/null
+++ b/db/migrate/20120620064014_add_references_to_export.rb
@@ -0,0 +1,7 @@
+class AddReferencesToExport < ActiveRecord::Migration
+ def change
+ change_table :exports do |t|
+ t.string :references_type, :reference_ids
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 7d1d92845..fe3bcd594 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120612071936) do
+ActiveRecord::Schema.define(:version => 20120620064014) do
create_table "access_links", :force => true do |t|
t.integer "access_point_id", :limit => 8
@@ -139,6 +139,8 @@ ActiveRecord::Schema.define(:version => 20120612071936) do
t.string "options"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "references_type"
+ t.string "reference_ids"
end
add_index "exports", ["referential_id"], :name => "index_exports_on_referential_id"
@@ -297,7 +299,6 @@ ActiveRecord::Schema.define(:version => 20120612071936) do
t.datetime "updated_at"
t.string "prefix"
t.string "projection_type"
- t.string "bounding_box", :limit => nil
t.string "time_zone"
end
diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb
index bccaa4c42..4e6a748ed 100644
--- a/spec/models/export_spec.rb
+++ b/spec/models/export_spec.rb
@@ -60,4 +60,92 @@ describe Export do
end
+ describe "#references" do
+
+ it "should be empty if references_type is nil" do
+ subject.references_type = nil
+ subject.references.should be_empty
+ end
+
+ it "should be empty if reference_ids is blank" do
+ subject.reference_ids = ""
+ subject.references.should be_empty
+ end
+
+ end
+
+ describe "#references=" do
+
+ context "with Lines" do
+
+ let(:lines) { create_list :line, 3 }
+
+ before(:each) do
+ subject.references = lines
+ end
+
+ it "should use 'Chouette::Line' as references_type" do
+ subject.references_type.should == 'Chouette::Line'
+ end
+
+ it "should use line identifiers as raw reference_ids" do
+ subject.raw_reference_ids.should == lines.map(&:id).join(',')
+ end
+
+ end
+
+ end
+
+ describe "#references_relation" do
+
+ it "should be 'lines' when relation_type is 'Chouette::Line'" do
+ subject.references_type = "Chouette::Line"
+ subject.references_relation.should == "lines"
+ end
+
+ it "should be 'networks' when relation_type is 'Chouette::Network'" do
+ subject.references_type = "Chouette::Network"
+ subject.references_relation.should == "networks"
+ end
+
+ it "should be nil when relation_type is blank" do
+ subject.references_type = ""
+ subject.references_relation.should be_nil
+ end
+
+ it "should be nil when relation_type is 'dummy'" do
+ subject.references_type = "dummy"
+ subject.references_relation.should be_nil
+ end
+
+ end
+
+ describe "#reference_ids" do
+
+ it "should parse raw_reference_ids and returns ids" do
+ subject.stub :raw_reference_ids => "1,2,3"
+ subject.reference_ids.should == [1,2,3]
+ end
+
+ it "should be empty if raw_reference_ids is blank" do
+ subject.stub :raw_reference_ids => ""
+ subject.reference_ids.should be_empty
+ end
+
+ end
+
+ describe "#reference_ids=" do
+
+ it "should join ids with comma" do
+ subject.reference_ids = [1,2,3]
+ subject.raw_reference_ids.should == "1,2,3"
+ end
+
+ it "should be nil if records is blank" do
+ subject.reference_ids = []
+ subject.raw_reference_ids.should be_nil
+ end
+
+ end
+
end