Skip to content

Instantly share code, notes, and snippets.

@selvait90
Created March 11, 2015 15:56
Show Gist options
  • Select an option

  • Save selvait90/cc1aac79ef19d0770c4f to your computer and use it in GitHub Desktop.

Select an option

Save selvait90/cc1aac79ef19d0770c4f to your computer and use it in GitHub Desktop.
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 The Collaborative Software Foundation
#
# This file is part of TriSano.
#
# TriSano is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# TriSano is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TriSano. If not, see http://www.gnu.org/licenses/agpl-3.0.txt.
class PlaceEntity < Entity
has_one :place, :foreign_key => "entity_id", :class_name => "Place", :dependent => :destroy
has_one :entity, :through => :place
accepts_nested_attributes_for :place
=begin
, :reject_if => proc { |attrs| attrs.all? { |k, v|
puts k; v.blank?
v.blank?
} }, :allow_destroy => true
=end
named_scope :jurisdictions,
:joins => "INNER JOIN places p on entities.id = p.entity_id INNER JOIN places_types on p.id = places_types.place_id INNER JOIN codes on places_types.type_id = codes.id",
:conditions => "codes.the_code = 'J' AND codes.code_name = 'placetype'",
:order => 'p.name',
:readonly => false
named_scope :labs,
:joins => "INNER JOIN places p on entities.id = p.entity_id INNER JOIN places_types on p.id = places_types.place_id INNER JOIN codes on places_types.type_id = codes.id",
:conditions => "codes.the_code = 'L' AND codes.code_name = 'placetype'",
:order => 'p.name',
:include => :place,
:readonly => false
named_scope :active,
:conditions => "entities.deleted_at IS NULL"
# Intended to be chained to one of the other jurisdiction named scopes
named_scope :excluding_unassigned,
:conditions => "p.name != 'Unassigned'"
named_scope :by_place_name, lambda { |place_name|
{ :joins => "INNER JOIN places p on entities.id = p.entity_id",
:conditions => ["p.name = ? and entities.deleted_at IS NULL", place_name]
}
}
named_scope :with_short_name, lambda { |short_name|
{ :conditions => ["p.short_name = ?", short_name] } }
named_scope :with_place_names_like, lambda { |place_name|
{ :joins => ["INNER JOIN places p on entities.id = p.entity_id"],
:conditions => ["p.name ILIKE ? AND entities.deleted_at IS NULL", '%' + place_name + '%']
}
}
named_scope :limit_by_place_types, lambda { |types|
{ :conditions => ["codes.the_code IN (?)", types] }
}
# Used to decrease number of queries executed when diplaying a place
# listing. Should be combined with other place scopes.
named_scope :optimize_for_index, {
:joins => [{:place => :place_types}],
:select => "DISTINCT(entities.id), p.name",
:order => "p.name ASC"
}
# builds a scoped object, like what is returned from named_scopes
def self.by_name_and_place_type(search_data)
scope = optimize_for_index
if search_data.includes_place_type?
scope = scope.limit_by_place_types(search_data.place_types)
end
scope = scope.with_place_names_like(search_data.name)
scope
end
# Convenience method to allow a place entity's place name to be accessible through
# the place entity directly, enabling things like collection_select to work easily.
def name
place.name unless place.nil?
end
def merge(entity_ids)
entity_ids = [entity_ids].flatten.compact
if entity_ids.empty?
errors.add(:base, :no_entities_for_merging)
return
end
begin
::PlaceEntity.transaction do
returning [] do |effected_participations|
self.class.exclude_deleted.exclude_entity(self).find(entity_ids).each do |entity|
effected_events_for_entity = []
pids = []
Participation.find(:all, :conditions => ["primary_entity_id = ? OR secondary_entity_id = ?", entity.id, entity.id]).each do |participation|
change_entity_reference(entity, participation)
update_addresses(participation)
participation.save!
effected_participations << participation
effected_events_for_entity << participation.event_id
end
pids = self.place.place_types.map { |p| p.id }
entity.place.place_types.each do |pt|
unless pids.include? pt.id
self.place.place_types << pt
end
end
entity.deleted_at = Time.now
entity.merged_into_entity_id = self.id
entity.merge_effected_events = effected_events_for_entity.uniq.join(",")
entity.save!
end
end
end
rescue Exception => ex
logger.error ex
errors.add(:base, :failed_merge, :msg => ex.message)
return nil
end
end
private
def change_entity_reference(entity, participation)
if participation.primary_entity_id == entity.id
participation.primary_entity_id = self.id
else
participation.secondary_entity_id = self.id
end
end
def update_addresses(participation)
if (participation.type == "InterestedPlace")
clone_canonical_address(participation)
elsif (participation.type == "InterestedParty")
change_address_entity_reference(participation)
end
end
# For certain participation types, the event's address should receive the canonical address of the entity
# at which the merge is directed.
def clone_canonical_address(participation)
if clone_canonical_address?(participation)
cloned_canonical_address = self.canonical_address.clone
cloned_canonical_address.save
delete_existing_address(participation)
participation.event.address = cloned_canonical_address
end
end
# Returns true if this participation's event requires a cloning of a canonical address, along with a merge operation.
def clone_canonical_address?(participation)
(self.canonical_address && (participation.type == "InterestedPlace"))
end
def change_address_entity_reference(participation)
existing_address = participation.event.address
unless existing_address.nil?
existing_address.entity = self
existing_address.save
end
end
def delete_existing_address(participation)
existing_address = participation.event.address
existing_address.destroy unless existing_address.nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment