From 04d456dfb262653951e521be315ff6877d26448e Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Tue, 14 Feb 2012 21:39:48 -0200 Subject: [PATCH] Sync city/state with Region --- app/models/community.rb | 3 +++ app/models/enterprise.rb | 3 +++ app/models/person.rb | 3 +++ lib/set_profile_region_from_city_state.rb | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 0 deletions(-) create mode 100644 lib/set_profile_region_from_city_state.rb diff --git a/app/models/community.rb b/app/models/community.rb index ba930ba..0cd68cb 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -10,6 +10,9 @@ class Community < Organization settings_items :language settings_items :zip_code, :city, :state, :country + extend SetProfileRegionFromCityState::ClassMethods + set_profile_region_from_city_state + before_create do |community| community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default') end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 8b07823..8bbc955 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -20,6 +20,9 @@ class Enterprise < Organization settings_items :organization_website, :historic_and_current_context, :activities_short_description, :zip_code, :city, :state, :country + extend SetProfileRegionFromCityState::ClassMethods + set_profile_region_from_city_state + before_save do |enterprise| enterprise.organization_website = enterprise.maybe_add_http(enterprise.organization_website) end diff --git a/app/models/person.rb b/app/models/person.rb index 0672022..dfa721f 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -177,6 +177,9 @@ class Person < Profile N_('Contact information'); N_('City'); N_('State'); N_('Country'); N_('Sex'); N_('Zip code') settings_items :photo, :contact_information, :sex, :city, :state, :country, :zip_code + extend SetProfileRegionFromCityState::ClassMethods + set_profile_region_from_city_state + def self.conditions_for_profiles(conditions, person) new_conditions = sanitize_sql(['role_assignments.accessor_id = ?', person]) new_conditions << ' AND ' + sanitize_sql(conditions) unless conditions.blank? diff --git a/lib/set_profile_region_from_city_state.rb b/lib/set_profile_region_from_city_state.rb new file mode 100644 index 0000000..41b96d0 --- /dev/null +++ b/lib/set_profile_region_from_city_state.rb @@ -0,0 +1,33 @@ +module SetProfileRegionFromCityState + + module ClassMethods + def set_profile_region_from_city_state + before_save :region_from_city_and_state + + include InstanceMethods + end + end + + module InstanceMethods + + def city=(value) + self.data[:city] = value + @change_region = true + end + + def state=(value) + self.data[:state] = value + @change_region = true + end + + def region_from_city_and_state + if @change_region + s = State.find_by_name self.state + c = s.children.find_by_name self.city + self.region = c + end + end + + end + +end -- libgit2 0.21.2