Skip to content

Instantly share code, notes, and snippets.

@magicmarkker
Created August 18, 2014 14:58
Show Gist options
  • Select an option

  • Save magicmarkker/a0b1d6bf1e6c4423dde9 to your computer and use it in GitHub Desktop.

Select an option

Save magicmarkker/a0b1d6bf1e6c4423dde9 to your computer and use it in GitHub Desktop.
class Efolder < ActiveRecord::Base
include Scopeable::Clientable
has_many :vfiles, as: :vfileable, dependent: :destroy
has_and_belongs_to_many :users, join_table: "efolders_users", foreign_key: "efolder_id"
has_and_belongs_to_many :committees
has_ancestry
validates :name, presence: true
before_save :share_me
after_save :share_subfolders
def hierarchy
self.ancestors.collect{|c| c.name}.push(self.name).join(" > ")
end
def to_public_hash(c, user)
f = {
"id" => self.id,
"name" =>self.name,
"shared" => (c == self.client_id) ? "0" : "1",
"content-count" => self.vfiles.length + self.children.size
}
f['has-permission'] = 1
f
end
def self.with_shared_scope(client_id)
client = Client.find(client_id)
parents = client.parent_ids
unscoped.where('(client_id in (?) and shared = 1) or client_id = ?', parents, client_id)
end
private
def share_me
self.shared = 0
if self.parent_id != 0
parent = Efolder.find(self.parent_id)
if parent.shared == 1
self.shared = 1
end
end
end
def share_subfolders
if self.shared = 1
self.children.each do |c|
c.update_attributes(shared: 1)
end
else
self.children.each do |c|
c.update_attribute(shared: 0)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment