Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ongs to an organization
  • Loading branch information...
commit c611da06c3da30bf829d6eb2f8f7106450795eee 1 parent 29044d3
authored October 05, 2012
7  app/models/folder.rb
@@ -30,10 +30,11 @@ def expiring_soon
30 30
     end
31 31
   end
32 32
 
33  
-  validates_uniqueness_of :name, :scope => [:parent_id, :user_id]
  33
+  validates_uniqueness_of :name, :scope => [:parent_id, :user_id, :organization_id]
34 34
 
35 35
   belongs_to :owner, :class_name => 'User', :foreign_key => 'user_id'
36 36
   belongs_to :audience, :class_name => 'Audience'
  37
+  belongs_to :organization
37 38
   has_many :folder_permissions
38 39
   has_many :authors, :through => :folder_permissions, :source => 'user', :conditions => proc{"permission = 1"}
39 40
   has_many :admins, :through => :folder_permissions, :source => 'user', :conditions => proc{'permission = 2'}
@@ -42,11 +43,11 @@ def expiring_soon
42 43
   attr_accessor :level, :ftype, :is_owner, :is_author, :leaf
43 44
 
44 45
   def users
45  
-    self.audience.recipients
  46
+    self.audience ? self.audience.recipients : []
46 47
     #User.scoped :joins => ', folders', :conditions => ['folders.audience_id IN (select * from sp_audiences_for_user(users.id)) and folders.id = ?', self.id]
47 48
   end
48 49
 
49  
-  acts_as_nested_set :scope => :user_id
  50
+  acts_as_nested_set :scope => [:user_id, :organization_id]
50 51
   
51 52
   accepts_nested_attributes_for :audience
52 53
 
9  app/models/organization.rb
@@ -35,6 +35,7 @@ class Organization < ActiveRecord::Base
35 35
   
36 36
   has_many :alert_attempts
37 37
   has_many :deliveries, :through => :alert_attempts
  38
+  has_one :folder
38 39
   belongs_to :contact, :class_name => "User", :foreign_key => :user_id
39 40
   has_paper_trail :meta => { :item_desc  => Proc.new { |x| x.to_s } }
40 41
 
@@ -44,7 +45,6 @@ class Organization < ActiveRecord::Base
44 45
   validates_presence_of :locality, :message => "City can't be blank"
45 46
   
46 47
   before_create :set_token, :create_group
47  
-  before_save :set_before_save_var
48 48
   after_save :ensure_folder
49 49
   
50 50
   scope :with_user, lambda {|user|
@@ -135,16 +135,11 @@ def set_token
135 135
   def create_group
136 136
     self.group = Group.create!(:scope => "Organization", :name => self.name)
137 137
   end
138  
-  
139  
-  def set_before_save_var
140  
-    @bs_name = self.name
141  
-  end
142  
-  
  138
+    
143 139
   # Ensure that there is a folder associated with this organization by
144 140
   # 1. Locating the folder by name, if one exists, creating one if not
145 141
   # 1. Ensuring that the audience holds the organization's audience and the contact is set as an admin
146 142
   def ensure_folder
147  
-    folder = Folder.first(:conditions => {:name => self.name == @bs_name ? self.name : @bs_name, :user_id => nil})
148 143
     if folder.nil?
149 144
       folder = Folder.new
150 145
     end
19  db/migrate/20121005201855_add_organization_id_to_folder.rb
... ...
@@ -0,0 +1,19 @@
  1
+class AddOrganizationIdToFolder < ActiveRecord::Migration
  2
+  def change
  3
+    change_table :folders do |t|
  4
+      t.integer :organization_id
  5
+    end
  6
+  end
  7
+  
  8
+  def migrate(direction)
  9
+    super
  10
+    
  11
+    if direction == :up
  12
+      execute "UPDATE folders
  13
+               SET organization_id = organizations.id
  14
+               FROM organizations
  15
+               WHERE folders.name = organizations.name
  16
+                 AND folders.user_id IS NULL"
  17
+    end
  18
+  end
  19
+end

0 notes on commit c611da0

Please sign in to comment.
Something went wrong with that request. Please try again.