Skip to content

Instantly share code, notes, and snippets.

@jwreagor
Created December 1, 2009 15:08
Show Gist options
  • Save jwreagor/246341 to your computer and use it in GitHub Desktop.
Save jwreagor/246341 to your computer and use it in GitHub Desktop.
require '../lib/flag_shih_tzu'
require 'rubygems'
require 'activerecord'
require 'sqlite3'
require 'irb'
ActiveRecord::Base.logger = Logger.new File.dirname(__FILE__) + "/perms.log"
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'perms.db'
ActiveRecord::Schema.define :version => 0 do
create_table :users, :force => true do |t|
t.belongs_to :user
t.string :name
end
create_table :tasks, :force => true do |t|
t.string :description
end
create_table :permissions, :force => true do |t|
t.belongs_to :user
t.belongs_to :task
t.integer :flags, :null => false, :default => 0
end
end
class Permission < ActiveRecord::Base
include FlagShihTzu
belongs_to :task
belongs_to :user
has_flags 1 => :viewable,
2 => :editable,
3 => :removable
end
class Task < ActiveRecord::Base
has_many :users, :through => :permissions
end
class User < ActiveRecord::Base
has_many :permissions
has_many :authored_tasks,
:class_name => Task.name
has_many :assigned_tasks,
:through => :permissions,
:source => :task
has_many :viewable_tasks,
:through => :permissions,
:conditions => Permission.viewable_condition,
:source => :task
has_many :editable_tasks,
:through => :permissions,
:conditions => Permission.editable_condition,
:source => :task
has_many :removable_tasks,
:through => :permissions,
:conditions => Permission.removable_condition,
:source => :task
end
::IRB.start(__FILE__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment