Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

small cleanup and refactoring

  • Loading branch information...
commit 5ba38846337b94c78a50b92b3a5a39665104ac78 1 parent a1e7508
@khustochka authored
View
8 app/controllers/taxonomy/familiae_controller.rb
@@ -1,10 +1,2 @@
class FamiliaeController < TaxaController
-
-# skip_before_filter :only => :index
-#
-# def index
-# redirect_to :controller => "ordines", :action => 'show', :id => params[:ordo_id]
-# end
-
-
end
View
8 app/controllers/taxonomy/taxa_controller.rb
@@ -1,17 +1,13 @@
class TaxaController < ApplicationController
class ArrayOfRecords < Array
- def nil_or_empty?
- self.nil? || self.empty?
- end
-
def cleanup(proceed_methods)
- unless self.nil_or_empty? || proceed_methods.nil? || proceed_methods.empty?
+ unless self.blank? || proceed_methods.blank?
proceed_method = proceed_methods.pop.keys.first
self.reject! do |item|
children = ArrayOfRecords.new(item.send(proceed_method))
children.cleanup(proceed_methods.dup)
- children.nil_or_empty?
+ children.blank?
end
end
end
View
4 app/helpers/taxa_helper.rb
@@ -8,9 +8,9 @@ def no_data_row
end
def hierarchy_cells(collection, proceed_methods = [], &block)
- if collection.nil? || collection.empty?
+ if collection.blank?
concat(no_data_row)
- elsif proceed_methods.nil? || proceed_methods.empty?
+ elsif proceed_methods.blank?
collection.each do |item|
yield item
end
View
34 app/models/taxonomy/taxon.rb
@@ -1,10 +1,12 @@
class Taxon < ActiveRecord::Base
- self.abstract_class = true # I've read the FM and got it!
+ self.abstract_class = true
validates_presence_of :name_la, :name_ru, :name_uk, :sort
validates_uniqueness_of :name_la, :name_ru, :name_uk
+ # Class methods
+
def self.prepare_hierarchy
proceed_methods = []
initial_model = self
@@ -20,30 +22,27 @@ def self.prepare_hierarchy
proceed_methods]
end
+ # Instance methods
+
def to_param
name_la
end
def insert_mind_sorting
- scope = ""
+ latest = (self.respond_to?(:supertaxon) ? self.supertaxon.subtaxa.count : self.class.count) + 1
+ self.sort = latest if self.sort > latest || self.sort == 0
+ conditions = ["sort >= #{self.sort}"]
if self.respond_to?(:supertaxon)
fk = self.supertaxon.class.to_s.foreign_key
- scope = " AND #{fk} = #{self[fk]}"
+ conditions.push("#{fk} = #{self[fk]}")
end
- latest = (self.respond_to?(:supertaxon) ? self.supertaxon.subtaxa.count : self.class.count) + 1
- self.sort = latest if self.sort > latest || self.sort == 0
self.class.transaction do
- self.class.update_all("sort = sort + 1", "sort >= #{self.sort}" + scope)
+ self.class.update_all("sort = sort + 1", conditions.join(" AND "))
save!
end
end
def update_mind_sorting(attributes)
- scope = ""
- if self.respond_to?(:supertaxon)
- fk = self.supertaxon.class.to_s.foreign_key
- scope = " AND #{fk} = #{self[fk]}"
- end
latest = self.respond_to?(:supertaxon) ? self.supertaxon.subtaxa.count : self.class.count
current = attributes[:sort].to_i
new_sort = attributes[:sort] =
@@ -57,21 +56,26 @@ def update_mind_sorting(attributes)
diff = (old_sort - new_sort) / (old_sort - new_sort).abs
max_sort = [old_sort, new_sort - diff].max
min_sort = [old_sort, new_sort - diff].min
- self.class.update_all("sort = sort + (#{diff})", "sort > #{min_sort} AND sort < #{max_sort}" + scope)
+ conditions = ["sort > #{min_sort}", "sort < #{max_sort}"]
+ if self.respond_to?(:supertaxon)
+ fk = self.supertaxon.class.to_s.foreign_key
+ conditions.push("#{fk} = #{self[fk]}")
+ end
+ self.class.update_all("sort = sort + (#{diff})", conditions.join(" AND "))
end
update_attributes!(attributes)
end
end
def destroy_mind_sorting
- scope = ""
+ conditions = ["sort > #{self[:sort]}"]
if self.respond_to?(:supertaxon)
fk = self.supertaxon.class.to_s.foreign_key
- scope = " AND #{fk} = #{self[fk]}"
+ conditions.push("#{fk} = #{self[fk]}")
end
self.class.transaction do
destroy
- self.class.update_all("sort = sort - 1", "sort > #{self[:sort]}" + scope)
+ self.class.update_all("sort = sort - 1", conditions.join(" AND "))
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.