Permalink
Browse files

much improved and fixed add/edit forms and logic for orders and famil…

…ies; added parent presence validation for SortedHierarchy
  • Loading branch information...
1 parent a0fd561 commit 5872bbce430304fd6c6d88ecd8e3d5a8b69ff12c @khustochka committed Mar 7, 2010
@@ -3,7 +3,7 @@ class SpeciesController < TaxaController
skip_before_filter :require_admin, :only => [:index, :show]
before_filter :find_taxon, :only => [:edit, :update, :destroy]
- before_filter :find_all_taxa, :only => [:new, :show, :edit]
+ before_filter :find_all_siblings_and_parents, :only => [:new, :show, :edit]
def index
respond_to do |format|
@@ -25,7 +25,7 @@ def cleanup(proceed_methods)
before_filter :find_taxon, :only => [:update, :destroy]
before_filter :find_taxon_with_children, :only => :show
- before_filter :find_all_taxa, :only => [:new, :show]
+ before_filter :find_all_siblings_and_parents, :only => [:new, :show]
before_filter :prepare_hierarchy, :only => :index
rescue_from ActiveRecord::RecordInvalid, :with => :rescue_invalid_record
@@ -45,8 +45,9 @@ def show
def new
- @taxon = model_class.new(:sort => @taxa.size + 1)
-
+ @taxon = model_class.new(:sort => @siblings.size + 1)
+ parent = params[model_class.parent_key]
+ @taxon[model_class.parent_key] = parent #unless parent.nil?
respond_to do |format|
format.html { render 'taxa/add_edit' }
#format.xml { render :xml => @taxon }
@@ -101,9 +102,19 @@ def find_taxon_with_children
@bunch = @taxon.children unless @taxon.bottom_level?
end
- def find_all_taxa
+ def find_all_siblings_and_parents
@proceed_methods = []
- @taxa = @taxon.nil? || @taxon.top_level? ? model_class.all(:order => "sort") : @taxon.parent.children
+ @siblings = if model_class.top_level?
+ model_class.all
+ elsif !@taxon.nil? && !@taxon[model_class.parent_key].nil?
+ @taxon.siblings_scope.all
+ elsif params[model_class.parent_key].nil?
+ []
+ else
+ model_class.siblings_scope(params[model_class.parent_key])
+ end
+
+ @parents = model_class.top_level? ? [] : model_class.parent_class.all
end
def prepare_hierarchy
@@ -113,7 +124,9 @@ def prepare_hierarchy
def rescue_invalid_record
respond_to do |format|
- find_all_taxa
+ find_all_siblings_and_parents
+ # TODO: maybe move nil or illegal sort transformation to valid last one into Sorted Hierarchy callback like before_validation ?
+ @taxon.sort = (@siblings.size + 1) if !@taxon[model_class.parent_key].nil? && (@taxon.sort.nil? || @taxon.changed.include?(model_class.parent_key.to_s))
format.html { render 'taxa/add_edit' }
#format.xml { render :xml => @taxon.errors, :status => :unprocessable_entity }
end
@@ -1,6 +1,8 @@
- form_for(item) do |f|
= f.error_messages
%p
+ = f.labelled_select(item.parent_key, item.parent_key, [["Please select parent", nil]] + @parents.map{|p| ["#{p.name_la} / #{p.name_ru}", p.id]}) unless item.top_level?
+ %p
= f.labelled_text_field :name_la
%p
= f.labelled_text_field :name_en
@@ -13,6 +15,6 @@
%p
= f.labelled_text_area :description
%p
- = f.labelled_set_sorting_combo "Insert after", items, item, "{:name_la} / {:name_ru}"
+ = items.empty? ? "Will be appended in the end." : f.labelled_set_sorting_combo("Insert after", items, item) {|sib| "#{sib.name_la} / #{sib.name_ru}"}
%p
= f.submit params[:id] ? 'Update' : 'Create'
@@ -11,6 +11,6 @@
%h2
Редактировать
-= render :partial => "taxa/form", :locals => { :item => @taxon, :items => @taxa }
+= render :partial => "taxa/form", :locals => { :item => @taxon, :items => @siblings }
= link_to "#{model_name.pluralize.humanize} index", :action => "index"
= " | " + link_to("Add new #{model_name}", :action => "new") if controller.action_name != 'new'
@@ -2,15 +2,15 @@ module ActionView::Helpers
class FormBuilder
- def set_sorting_combo(bunch, one, label_format = "sort number: {:sort}")
- select( :sort, [["At the beginning", 1]] + (bunch - [one]).map {|tx| [label_format.class.eql?(Symbol) ? tx[label_format] : label_format.gsub(/\{:([A-Za-z_]+)\}/) {|prp| tx[$1.to_sym]}, tx[:sort] + (tx[:sort] < one[:sort] ? 1 : 0)]} )
+ def set_sorting_combo(bunch, one, &block)
+ select( :sort, [["At the beginning", 1]] + (bunch - [one]).map {|tx| [block_given? ? (yield tx) : "sort number: #{tx[:sort]}", tx[:sort] + (tx[:sort] < one[:sort] ? 1 : 0)]} )
end
alias :original_method_missing :method_missing
- def method_missing(method_name, *args)
+ def method_missing(method_name, *args, &block)
if method_name.to_s.include?('labelled_')
- label(args[0]) + "\n<br />\n" + send(method_name.to_s.gsub(/labelled_(.*)/, '\1').intern, *(args[1].nil? ? args : args[1..-1]))
+ label(args[0]) + "\n<br />\n" + send(method_name.to_s.gsub(/labelled_(.*)/, '\1').intern, *(args[1].nil? ? args : args[1..-1]), &block)
else
original_method_missing(method_name, *args)
end
@@ -33,6 +33,7 @@ def child_of(association_id, options = {})
{:conditions => "#{parent_key} = #{fk}"}
}
default_scope :order => get_sort_column
+ validate :parent_existence
end
def set_sort_column(value)
@@ -91,6 +92,10 @@ def siblings_count
private
+ def parent_existence # effective only for child
+ errors.add(parent_key, :invalid, :value => self[parent_key]) if self.class.parent_class.find_by_id(self[parent_key]).nil?
+ end
+
def correctness_of_sort_value
raw_value = send("#{get_sort_column}_before_type_cast")
unless raw_value.nil?
@@ -107,7 +112,7 @@ def correctness_of_sort_value
def give_way_to_create
latest = siblings_count + 1
- self[get_sort_column] ||= latest
+ self[get_sort_column] = latest if self[get_sort_column].nil? || self.changed.include?(parent_key.to_s)
if self[get_sort_column] < latest
siblings_scope.update_all("#{get_sort_column} = #{get_sort_column} + 1", "#{get_sort_column} >= #{self[get_sort_column]}")
end

0 comments on commit 5872bbc

Please sign in to comment.