Skip to content

Instantly share code, notes, and snippets.

@StlTenny
Created March 5, 2012 21:43
Show Gist options
  • Save StlTenny/1981303 to your computer and use it in GitHub Desktop.
Save StlTenny/1981303 to your computer and use it in GitHub Desktop.
StlTenny Models
class Message < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :role, :remember_me
#Setup relationship between user and email
has_many :messages, :dependent => :destroy
ROLES = %w[admin subsc guest]
#named_scope :with_role, lambda { |role| { :conditions => ["role = ?", role] }}
def role?(role)
return self.role == role.to_s
end
def admin?
return self.role == "admin"
end
end
class MessagesController < AuthorizedController
before_filter :authenticate_owner!
load_and_authorize_resource :through => :current_user
# GET /messages
# GET /messages.json
# index, show, new, edit, update, destroy
def index
@messages = @current_user.messages
end
def show
@message = @current_user.messages.find(params[:id])
end
def new
@message = @current_user.messages.build
end
def create
@message = @current_user.messages.build(params[:message])
if @message.save
MessageMailer.message_send(@message).deliver
flash[:notice] = "Successfully Sent Message"
redirect_to user_url(@message.user_id)
else
render :action => :new
end
end
def edit
@message = @Messages.find(params[:id])
end
def update
@message = @Messages.find(params[:id])
if @message.update_attributes(params[:message])
flash[:notice] = "Successfully updated message."
redirect_to user_url
else
render :action => :edit
end
end
def destroy
@message = @Messages.find(params[:id])
@message.destroy
flash[:notice] = "Successfully destroyed meessage."
redirect_to user_url(@message.user_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment