aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorMichel Etienne2012-07-09 23:02:20 +0200
committerMichel Etienne2012-07-09 23:02:20 +0200
commit3cf46d6cbe8f490ad1338ae40bf4d1ec8fcbd2d4 (patch)
treecf8415ac4f8816be35c9768cd6c9eadd0504036d /app
parentea483438db92d2d8c46717e2fc20ea1417e2c4b6 (diff)
downloadchouette-core-3cf46d6cbe8f490ad1338ae40bf4d1ec8fcbd2d4.tar.bz2
add clean up process
Diffstat (limited to 'app')
-rw-r--r--app/assets/stylesheets/referentials.css.scss31
-rw-r--r--app/controllers/clean_ups_controller.rb24
-rw-r--r--app/models/clean_up.rb123
-rw-r--r--app/views/referentials/show.html.erb18
4 files changed, 194 insertions, 2 deletions
diff --git a/app/assets/stylesheets/referentials.css.scss b/app/assets/stylesheets/referentials.css.scss
index fe38f4f81..2d916232f 100644
--- a/app/assets/stylesheets/referentials.css.scss
+++ b/app/assets/stylesheets/referentials.css.scss
@@ -24,9 +24,36 @@
.count td.value {
text-align: right;
}
-
+
+}
+#sidebar div#clean_up {
+ form {
+ border-bottom: 1px solid #BBB;
+ fieldset {
+ padding: 0;
+ border: 0;
+ li.date {
+ padding: 0;
+ label { padding-left: 10px;
+ width: 40%;}
+ input { width: 42%; }
+ }
+ li {
+ padding: 0;
+ label { padding-left: 10px;
+ width: 90%;}
+ input { width: 12%; }
+ }
+ }
+ fieldset.actions {
+ margin-bottom: 0;
+ padding-left: 20px;
+ }
+ fieldset.inputs {
+ padding-top: 16px;
+ }
+ }
}
-
ul.logos {
margin: 30px 30px 0 0;
diff --git a/app/controllers/clean_ups_controller.rb b/app/controllers/clean_ups_controller.rb
new file mode 100644
index 000000000..2d270c178
--- /dev/null
+++ b/app/controllers/clean_ups_controller.rb
@@ -0,0 +1,24 @@
+class CleanUpsController < ChouetteController
+ respond_to :html, :only => [:create]
+
+ belongs_to :referential
+
+ def create
+ begin
+ clean_up = CleanUp.new( params[:clean_up])
+ clean_up.clean
+ #flash[:notice] = "Message 1<br />"
+ #flash[:notice] << "Message 2"
+ notice = t('clean_ups.success_tm', :tm_count => clean_up.time_table_count.to_s)
+ if (clean_up.vehicle_journey_count > 0)
+ notice += t('clean_ups.success_vj', :vj_count => clean_up.vehicle_journey_count.to_s)
+ end
+ flash[:notice] = notice
+ rescue
+ flash[:alert] = t('clean_ups.failure')
+ end
+ redirect_to referential_path(@referential)
+
+ end
+
+end
diff --git a/app/models/clean_up.rb b/app/models/clean_up.rb
new file mode 100644
index 000000000..2d8e4c693
--- /dev/null
+++ b/app/models/clean_up.rb
@@ -0,0 +1,123 @@
+class CleanUp
+ include ActiveModel::Validations
+ include ActiveModel::Conversion
+ extend ActiveModel::Naming
+
+
+ attr_accessor :expected_date, :keep_lines, :keep_stops , :keep_companies, :keep_networks
+ attr_accessor :time_table_count,:vehicle_journey_count,:journey_pattern_count,:route_count,:line_count
+ attr_accessor :stop_count,:company_count,:network_count
+
+ validates_presence_of :expected_date
+
+ def initialize(attributes = {})
+ attributes.each do |name, value|
+ send("#{name}=", value)
+ end
+ end
+
+ def persisted?
+ false
+ end
+
+ def clean
+
+ # find and remove time_tables
+ tms = Chouette::TimeTable.expired_on(Date.parse(expected_date))
+ self.time_table_count = tms.size
+ tms.each do |tm|
+ tm.destroy
+ end
+ # remove vehiclejourneys without timetables
+ self.vehicle_journey_count = 0
+ Chouette::VehicleJourney.find_each do |vj|
+ if vj.time_tables.size == 0
+ self.vehicle_journey_count += 1
+ vj.destroy
+ end
+ end
+ # remove journeypatterns without vehicle journeys
+ self.journey_pattern_count = 0
+ Chouette::JourneyPattern.find_each do |jp|
+ if jp.vehicle_journeys.size == 0
+ self.journey_pattern_count += 1
+ jp.destroy
+ end
+ end
+ # remove routes without journeypatterns
+ self.route_count = 0
+ Chouette::Route.find_each do |r|
+ if r.journey_patterns.size == 0
+ self.route_count += 1
+ r.destroy
+ end
+ end
+ # if asked remove lines without routes
+ self.line_count = 0
+ if keep_lines == "0"
+ Chouette::Line.find_each do |l|
+ if l.routes.size == 0
+ self.line_count += 1
+ l.destroy
+ end
+ end
+ end
+ # if asked remove stops without children (recurse)
+ self.stop_count = 0
+ if keep_stops == "0"
+ Chouette::StopArea.find_each(:conditions => { :area_type => "BoardingPosition" }) do |bp|
+ if bp.stop_points.size == 0
+ self.stop_count += 1
+ bp.destroy
+ end
+ end
+ Chouette::StopArea.find_each(:conditions => { :area_type => "Quay" }) do |q|
+ if q.stop_points.size == 0
+ self.stop_count += 1
+ q.destroy
+ end
+ end
+ Chouette::StopArea.find_each(:conditions => { :area_type => "CommercialStopPoint" }) do |csp|
+ if csp.children.size == 0
+ self.stop_count += 1
+ csp.destroy
+ end
+ end
+ Chouette::StopArea.find_each(:conditions => { :area_type => "StopPlace" }) do |sp|
+ if sp.children.size == 0
+ self.stop_count += 1
+ sp.destroy
+ end
+ end
+ Chouette::StopArea.find_each(:conditions => { :area_type => "ITL" }) do |itl|
+ if itl.routing_stops.size == 0
+ self.stop_count += 1
+ itl.destroy
+ end
+ end
+ end
+ # if asked remove companies without lines or vehicle journeys
+ self.company_count = 0
+ if keep_companies == "0"
+ Chouette::Company.find_each do |c|
+ if c.lines.size == 0
+ self.company_count += 1
+ c.destroy
+ end
+ end
+ end
+
+ # if asked remove networks without lines
+ self.network_count = 0
+ if keep_networks == "0"
+ Chouette::Network.find_each do |n|
+ if n.lines.size == 0
+ self.network_count += 1
+ n.destroy
+ end
+ end
+ end
+
+ end
+
+end
diff --git a/app/views/referentials/show.html.erb b/app/views/referentials/show.html.erb
index f6c9522ce..ba12da4b4 100644
--- a/app/views/referentials/show.html.erb
+++ b/app/views/referentials/show.html.erb
@@ -96,4 +96,22 @@
<li><%= link_to t('referentials.actions.destroy'), referential_path(@referential), :method => :delete, :confirm => t('referentials.actions.destroy_confirm'), :class => "remove" %></li>
<br>
</ul>
+
+ <h3><%= t('.clean_up') %></h3>
+ <div id="clean_up" >
+ <%= semantic_form_for [@referential, CleanUp.new] do |form| %>
+ <%= form.inputs do %>
+ <%= form.input :expected_date, :as => :date_picker , :wrapper_html => { :class => 'date' }%>
+ <%= form.input :keep_lines, :as => :boolean %>
+ <%= form.input :keep_stops, :as => :boolean %>
+ <%= form.input :keep_companies, :as => :boolean %>
+ <%= form.input :keep_networks, :as => :boolean %>
+ <% end %>
+ <%= form.actions do %>
+ <%= form.action :submit, :as => :button, :label => t('clean_ups.actions.clean_up') , :button_html => {:confirm => t('clean_ups.actions.confirm') }%>
+ <% end %>
+ <% end %>
+ </div>
+
+
<% end %>