Permalink
Browse files

adding orgnaization_id to folders table to indicate that a folder bel…

…ongs to an organization
  • Loading branch information...
1 parent 29044d3 commit c611da06c3da30bf829d6eb2f8f7106450795eee cdubose committed Oct 5, 2012
Showing with 25 additions and 10 deletions.
  1. +4 −3 app/models/folder.rb
  2. +2 −7 app/models/organization.rb
  3. +19 −0 db/migrate/20121005201855_add_organization_id_to_folder.rb
@@ -30,10 +30,11 @@ def expiring_soon
end
end
- validates_uniqueness_of :name, :scope => [:parent_id, :user_id]
+ validates_uniqueness_of :name, :scope => [:parent_id, :user_id, :organization_id]
belongs_to :owner, :class_name => 'User', :foreign_key => 'user_id'
belongs_to :audience, :class_name => 'Audience'
+ belongs_to :organization
has_many :folder_permissions
has_many :authors, :through => :folder_permissions, :source => 'user', :conditions => proc{"permission = 1"}
has_many :admins, :through => :folder_permissions, :source => 'user', :conditions => proc{'permission = 2'}
@@ -42,11 +43,11 @@ def expiring_soon
attr_accessor :level, :ftype, :is_owner, :is_author, :leaf
def users
- self.audience.recipients
+ self.audience ? self.audience.recipients : []
#User.scoped :joins => ', folders', :conditions => ['folders.audience_id IN (select * from sp_audiences_for_user(users.id)) and folders.id = ?', self.id]
end
- acts_as_nested_set :scope => :user_id
+ acts_as_nested_set :scope => [:user_id, :organization_id]
accepts_nested_attributes_for :audience
@@ -35,6 +35,7 @@ class Organization < ActiveRecord::Base
has_many :alert_attempts
has_many :deliveries, :through => :alert_attempts
+ has_one :folder
belongs_to :contact, :class_name => "User", :foreign_key => :user_id
has_paper_trail :meta => { :item_desc => Proc.new { |x| x.to_s } }
@@ -44,7 +45,6 @@ class Organization < ActiveRecord::Base
validates_presence_of :locality, :message => "City can't be blank"
before_create :set_token, :create_group
- before_save :set_before_save_var
after_save :ensure_folder
scope :with_user, lambda {|user|
@@ -135,16 +135,11 @@ def set_token
def create_group
self.group = Group.create!(:scope => "Organization", :name => self.name)
end
-
- def set_before_save_var
- @bs_name = self.name
- end
-
+
# Ensure that there is a folder associated with this organization by
# 1. Locating the folder by name, if one exists, creating one if not
# 1. Ensuring that the audience holds the organization's audience and the contact is set as an admin
def ensure_folder
- folder = Folder.first(:conditions => {:name => self.name == @bs_name ? self.name : @bs_name, :user_id => nil})
if folder.nil?
folder = Folder.new
end
@@ -0,0 +1,19 @@
+class AddOrganizationIdToFolder < ActiveRecord::Migration
+ def change
+ change_table :folders do |t|
+ t.integer :organization_id
+ end
+ end
+
+ def migrate(direction)
+ super
+
+ if direction == :up
+ execute "UPDATE folders
+ SET organization_id = organizations.id
+ FROM organizations
+ WHERE folders.name = organizations.name
+ AND folders.user_id IS NULL"
+ end
+ end
+end

0 comments on commit c611da0

Please sign in to comment.