This started with the help of this Rails Messaging Tutorial which I borrowed a used a couple lines of code from this tutorial to extend this project. So many thanks to manitoba98 and the guys at PingMe for writing acts_as_network.

There are so many things acts_as_network can be used for. So far I have used acts_as_network to allow users to be able to invite friends as well as receive friend requests from other members. It was also used to implement a way for users to block other users and I am using it for my messaging system. So I thought I would put this out there just to kind of show anyone who might be interested to see just how great acts_as_network is.

So I will assume you have installed the plugin. Follow the link above if you cannot find it otherwise. I am also using restful_authentication but you can use whatever.

After you install the plugin create a new model and controller

script/generate model Mail user_id:integer user_id_target:integer subject:string body:text recipient_deleted:boolean author_deleted:boolean

script/generate controller Messages index show new edit

In the migration setup

t.boolean :author_deleted, :default => ‘false’

t.boolean :recipient_deleted, :default => ‘false’

Now in your User Model add the association

class User < ActiveRecord::Base

   acts_as_network :messages, :through => :mails,

end

Now in the Mail Model

class Mail < ActiveRecord::Base
  belongs_to  :user
  belongs_to  :user_target, :class_name => ‘User’, :foreign_key => ‘user_id_target’
  validates_presence_of :user, :user_target, :body

Setup your Controller to look something like this…

class MessagesController < ApplicationController
  before_filter :login_required

  def index
    @messages = (params[:folder] != ’sent’) ? current_user.mails_in : current_user.mails_out
    @folder = params[:folder]
  end
  def show
    @message = Mail.find(params[:id])
    @user = User.find(@message.user_id)
  end

  def new
    @message = Mail.new
    @target = User.find(params[:user_target])
  end
  def edit
    @message = Mail.find(params[:id])
    @subject = @message.subject.sub(/^(Re: )?/, “Re: “)
    @target = User.find(@message.user_id)
  end
  def create
    @message = Mail.new(:user => current_user, :user_target => User.find(params[:user_target]), :subject => params[:subject], :body => params[:body])
    @message.save ? redirect_to(inbox_path) : render(:action => ‘new’, :user_targer => params[:user_target])   
  end
  def update
    @message = Mail.find(params[:id])
    @message.toggle!(:recipient_deleted) ? redirect_to(inbox_path) : redirect_to(:action => ’show’, :id => params[:id])
  end
  def destroy
    @message = Mail.find(params[:id])
    if @message.user_id == current_user.id
      @message.update_attribute(”author_deleted”, true)
    elsif @message.user_id_target == current_user.id
      @message.update_attribute(”recipient_deleted”, true)     
    elsif @message.user_id == current_user.id && @message.user_id_target == current_user.id
      @message.update_attributes(”recipient_deleted” => true, “author_deleted” => true)
    end
    redirect_to inbox_path
  end

end

The index.html.erb I am about to show you is for an example only. It’s wasn’t written with DRY in mind. I’ll post some better looking code at some later point but for now this is what it is.

<table width=”100%” border=”0″>
    <tr>
        <th>Login</th>
        <th>Subject</th>
        <th>Sent</th>
    </tr>
        <% @messages.each do |message| %>
            <% user = (@folder != ’sent’)? User.find(message.user_id) : User.find(message.user_id_target) %>
            <% if @folder == ‘inbox’ || @folder.blank? %>
                <% if message.recipient_deleted == false %>
                    <tr>
                        <td><%= link_to user.login, user_path(user) %></td>
                        <td><%= link_to message.subject, message_path(message) %></td>
                        <td><%= distance_of_time_in_words(message.created_at, Time.now) %> ago</td>
                    </tr>
                <% end %>
            <% elsif @folder == ’sent’ %>
                <% if message.author_deleted == false %>
                    <tr>
                        <td><%= link_to user.login, user_path(user) %></td>
                        <td><%= link_to message.subject, message_path(message) %></td>
                        <td><%= distance_of_time_in_words(message.created_at, Time.now) %> ago</td>
                    </tr>
                <% end %>
            <% elsif @folder == ‘trash’ %>
                <% if message.recipient_deleted == true %>
                    <tr>
                        <td><%= link_to user.login, user_path(user) %></td>
                        <td><%= link_to message.subject, message_path(message) %></td>
                        <td><%= distance_of_time_in_words(message.created_at, Time.now) %> ago</td>
                    </tr>
                <% end %>               
            <% end %>
        <% end %>
</table>
</div>

edit.html.erb

<% form_tag ‘/messages/create’ do %>
    <ul>
        <li><label for=”user_target”>To:</label> <%=h @target.login %><%= hidden_field_tag ‘user_target’, @target.id %></li>
        <li><label for=”subject”>Subject:</label> <%= text_field_tag ’subject’, @subject %></li>
        <li><label for=”body”>Body:</label><br /> <%= text_area_tag ‘body’, @body %></li>
        <li><%= submit_tag ‘Create’ %></li>
    </ul>
<% end %>

new.html.erb

<div id=”message_new”>
<% form_tag ‘/messages/create’ do %>
    <ul>
        <li><label for=”user_target”>To:</label> <%=h @target.login %><%= hidden_field_tag ‘user_target’, @target.id %></li>
        <li><label for=”subject”>Subject:</label> <%= text_field_tag ’subject’, ” ,:class=>”text”  %></li>
        <li><label for=”body”>Body:</label><br /> <%= text_area_tag ‘body’ %></li>
        <li><%= submit_tag ‘Create’ %></li>
    </ul>
<% end %>
</div>

show.html.erb

<div id=”message_show”>
    <ul>
        <li>Subject: <%=h @message.subject %></li>
        <li>From: <%=h @user.login %></li>
        <li class=”body”>Body:<br /><br /><%= @message.body %></li>
        <li><%= link_to ‘Reply’, edit_message_path %> | <%= link_to ‘Trash’, message_path(@message), :method => :delete %></li>
    </ul>
</div>

routes.rb:     map.resources :messages

                   map.inbox ‘/inbox’, :controller => ‘messages’, :action => ‘index’, :folder => ‘inbox’

                  map.sent  ‘/sent’, :controller => ‘messages’, :action => ‘index’, :folder => ’sent’

And that should about do it.

 

The project I am working on pretty much revolves around a user’s profile. I am still pretty new to working with Rails but, I knew there was an easier way of implementing this without having to add columns to the user table. I looked around for awhile for a tutorial or blog suggesting a painless way of adding this to an application.
I really wasn’t able to find anything so I decided to write this mini-how to.

I would first recommend that you take a look at Raaum’s Rails Reader. There is some really great documentation about ActiveRecord and how associations work. It would behoove you to follow along creating the user models and the migrations following along with the text because things will make sense that way. If you are lazy like me creating a bunch of junk models that you are just going to destroy sounds tedious I know but, it’s definitely worth the effort in the long run.

Nevertheless, I needed to be able to call a user’s profile within current_user. For example current_user.profile.body. So assuming you have already created your user model and setup the inclusion within you Application Controller. Now create your profile model and the migration accordingly.

script/generate model Profile user_id:integer body:text

Now open the Profile controller and add has_one :user somewhere after the class declaration.

class Profile < ActiveRecord::Base
has_one :user
end

It is work mentioning at this point that within the scope of this text the user will be created then redirected to the edit their profile. So if you plan to add validations in your model (ie: validates_presence_of) make sure to include

validates_presence_of :your_fields, :on => :update

Otherwise you will be unable to save the profile later.
Now add the same has_one statement to your User model but you will use

class User < ActiveRecord::Base
has_one :profile

Here is an example of how your controller might look…

class UsersController < ApplicationController
after_filter :update_session, :only => :create
before_filter :login_required, :only => [:edit, :show, :update]
before_filter :logout_required, :only => [:new, :create]
def index
@users = User.find(:all, :conditions => ['status = ?', 'active'])
end
def show
@user = User.find_by_login(params[:id])
end
def create
@user = User.new(params[:user])
@user.profile = Profile.new
self.current_user = (@user.save!)? @user : render(:action => ‘new’)
redirect_to :action => ‘edit’, :id => self.current_user.login
flash[:notice] = ‘Account successfully created!’
end
def update
self.current_user.profile.update_attributes!(params[:profile])? redirect_to(:action => ’show’, :id => self.current_user.login) : render(:action => ‘edit’)
end
end

As you can see when the create method is called it creates the Profile model and after the user is saved then redirected to the edit template.

This was the solution that worked best for me.

This fun street parade woke me up this morning.

Tags:

Cops: Florida Boy, 13, Killed Brother, 8, Over Dessert:
ORLANDO, Fla. — A 13-year-old choked and beat his 8-year-old brother to death because the younger boy ate a dessert and the older one worried he would be blamed, authorities said Wednesday.
Demetrius Key was arrested on first-degree murder. The boys’ mother, Tangela Key, told police she was visiting a cousin nearby and left him in charge of Levares Key and other younger siblings Saturday.
A neighbor told investigators she heard four loud bangs, followed by 10 minutes of quiet and then more commotion, according to an Orange County Sheriff’s Office news release.
Demetrius Key went to the cousin’s house and told his mother his brother was “passed out,” the sheriff’s office said. The younger boy was pronounced dead at a hospital.
Demetrius Key initially said he hit his brother with a metal shelf support, investigators said. After investigators searched the house, he said he used a broom handle, the sheriff’s office said.
He then told the detective he punched the boy, choked him and banged his head on the floor, according to an affidavit.
“Demetrius offered that Levares upset him by eating a dessert that (he) was not to have eaten,” Detective Appling Wells wrote. “He also advised Levares upset him by picking a scab and causing it to bleed.
“Demetrius said he feared Levares would blame both circumstances on him and tell his mother he had struck him and eaten the dessert.”
The sheriff’s office would not say what the dessert was.

Tags:

NYTimes.com:

“We’re cutting off many heads,” Steve Robertson, a special agent and spokesman for the D.E.A., said yesterday.

“Will new heads grow back? Yeah. That’s the nature of the drug business.”

I guess it’s just another game of Whack-a-Mole to these guys.

Tags:

To Whom It May Concern:

We have received your request for additional documentation to remove any restricted access which has been placed on our account. Your request was carefully considered. However, we regret we are unable to submit copies of invoices from our supplier(s) without first having a Nondisclosure Agreement from your firm.

Additionally, the privacy of our customers, suppliers, and any third parties, which we might conduct business, is very important to us. We are currently reviewing any Privacy or Confidentiality Agreement we have with our supplier(s) to make sure there is not a breach of contract providing this information to your firm.

With your cooperation of providing us with a Nondisclosure Agreement we will provide the requested documentation. Otherwise we would regrettably have to request our account be formally closed and the remaining balance be returned. You may deduct any monies owed to Ebay, Inc. providing an invoice is submitted along with your instrument.

Thank you for your efforts in resolving this matter.

Sincerely,

Nathan Wilson

How about Sen Larry Craig? If it were not for all of these angry back-stabbing republicans are flapping their jaws for this man’s resignation only because they are just bitter there wasn’t enough time to get a wiretap started for ol’ Larry before the media got wind of what happened. Then maybe this man might have been able to keep his job. I guess their cover up scandal has not come up to bat yet so they should point the finger at the other guy while they can.

But let us compare the mule used for covering up who really leaked the identity of a CIA agent to the media and trying to keep the fact your bathroom stall had been raided by an over zealous beat cop.

I am Gay, I don’t agree with anything this man politics however, I read the transcript of the interview Craig had with the Police about the investigation. It appeared to me the Police kept leading the conversation into making it look like the Defendant was trying to lie.

I am going to ask that people open yourselves up to at least the possibility that this Hall Monitor, I mean Beat Cop working the Bathroom Stall scene could have very well seen Craig go to the men’s bathroom thought this guy could be his ticket out of Bathroom Patrol and followed behind him, then from the next stall over then managed to see enough that could be written into a Police Report to satisfy an arrest.

I know the Idaho Press-Tribune is known for their investigative research did manage to publish an article about the “reliable reputation” of this officer but, I don’t for a moment believe this man is not envied by the other Beat Cops.

Everyone who is so quick to demand the resignation of this man is foolish to not at least consider the possibility of Craig having ticket out of the Cruising Gay Sex  Spots.