Categories
ruby on rails

Fetching value of hidden input field in capybara

There can be many instances in testing with capybara where you might need to interact with hidden fields. By default capybara’s

find_field

selector ignores hidden fields.
To get the value of the hidden field you can use

page.all
page.all("input['name=you_input_name']", :visible => false).first.value
# you have to use first as it returns and array of element matching the query criteria

To set the value of the hidden field you can use

page.all("input['name=you_input_name']", :visible => false).first.set(value_to_set)
Categories
ruby on rails

Customizing Mailboxer mailer views

To copy the mailboxer mailer views to your rails app run

rails g mailboxer:views

This will generate two folders, message_mailer and notification_mailer with html & text views for messages, notification and message replies respectively.

Next create a modify the initializer of mailboxer to include the following config

Mailboxer.setup do |config|
  #Enables or disables email sending for Notifications and Messages
  config.uses_emails = true
  #Configures the default `from` address for the email sent for Messages and Notifications of Mailboxer
  config.default_from = "admin@yoursite.com"
end

Add the following method to your messageable model

  #Returning the email address of the model if an email should be sent for this object (Message or Notification).
  #If no mail has to be sent, return nil.
  def mailboxer_email(object)
  #Check if an email should be sent for that object
  #if true
    return "define_email@on_your.model"
  #if false
  #return nil
  end

To change the title of you mail messages

open config/locales/en.yml and you will see the following line belonging to each mail you can the below line to suit to your needs

en:
  mailboxer:
    message_mailer:
      subject_new: "Mailboxer new message: %{subject}"
      subject_reply: "Mailboxer new reply: %{subject}"
    notification_mailer:
      subject: "Mailboxer new notification: %{subject}"
Categories
ruby ruby on rails

Adding TinyMce editor to Rails Admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data. Rails admin is like bootstrap admin interface for your web app / site. To add tinymce editor to all textareas in rails_admin first add the tinymce-rails gem to your Gemfile and run bundle install.

Gemfile

gem 'tinymce-rails', :git => 'git://github.com/spohlenz/tinymce-rails.git', :branch => 'tinymce-4'

Now got to app/assests/javascripts/rails_admin folder and include the tinymce javascript libraries to rails_admin.js.erb file like this

//= require 'tinymce-jquery'

Now create a javascript file called tinymce.js in app/assets/javascripts/rails_admin folder and add the following code to invoke tinymce on all textareas

function tinymce_load(){
  tinymce.init({
    selector: "textarea",
    plugins: [
     "advlist autolink lists link image charmap print preview anchor",
     "searchreplace visualblocks code fullscreen autoresize",
     "insertdatetime media table contextmenu paste"
    ],
    menu : "core",
    toolbar: "undo redo | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link"
  });
}
$(window).load(tinymce_load());
$(document).on('pjax:complete', tinymce_load );

A note on pjax line. RailsAdmin uses pjax for better user experience so we have to trigger the tinymce_load function when ever pjax is called so that the new textareas are also tinymced!

then include this file in rails_admin.js.erb file like

//= require 'rails_admin/tinymce'

Now restart your server and you have tinymce loading on all textareas. For more customization options of tinymce please refer their website.

 

Categories
ruby ruby on rails

A treatise on mailboxer ruby gem

Mailbox Gem

Mailboxer gem adds messaging capability to your models.By far of all messaging gems we have found this one easy to implement / add to your models with good documentation.

Mailboxer Installation

In you rails application add

gem "mailboxer", "~> 0.9.0"

to your Gemfile and run bundle install to install it.

Now go to rails application directory and run to generate the necessary migrations and then migrate the changes to your database

rails g mailboxer:install
rake db:migrate

Configuring Maiboxer

In rails you can create an intializer called mailboxer.rb in folder /config/intializer and modify the default behaviour of mailboxer. An example intializer. In our setup we are not interested in user emails. so we have just disabled it. Mailboxer automatically generates a initializer for you can go and change the default settings.

Mailboxer.setup do |config|

  #Configures if you applications uses or no the email sending for Notifications and Messages
  config.uses_emails = false

  #Configures the default from for the email sent for Messages and Notifications of Mailboxer
  config.default_from = "no-reply@mailboxer.com"

  #Configures the methods needed by mailboxer
  # These methods must be implemented in the model which will be using mailboxer
  config.email_method = :email # This method is needed only if you are going to send an email for each converstion / replies
  config.name_method = :name # This method is needed if you want to user mailboxer

  #Configures if you use or not a search engine and wich one are you using
  #Supported enignes: [:solr,:sphinx]
  config.search_enabled = false
  config.search_engine = :solr
end

Preparing the Model

In your user model add acts_as_messageable and implement the method name as shown below

class User < ActiveRecord::Base
  attr_accessible :email, :first_name, :last_name
  acts_as_messageable

  # Returns the name of the user
  def name
   return "#{first_name} #{last_name}"
  end
end

Basic Messaging

In mailboxer when an user send message to another user, a conversation is started. So there will be a notification is created with the content of the message and a receipt of notification is add to sent box of the sending user and a receipt of notification is added to the inbox of receiving user.

mailboxer flow (1)

To send a message to user2 from user1

user1.send_message(user2, body_of_the_message, subject_of_the_message)

#sending a message

To send message to multiple users use (just pass and array of recipients to send_message function.

user1.send_message([user2, user3, user4], body_of_the_message, subject_of_the_message)

To reply to the message

#message reply
user2.reply_to_conversation(conversation, "Reply body")

#Reply to all in the conversation (if the conversation involves more than two users )
user2.reply_to_all(receipt, "Reply body")

#To reply to the message sender
user2.reply_to_sender(receipt, "Reply body")

Retrieve all messages of the user

#user1 wants to retrieve all his conversations
user1.mailbox.conversations

#user1 wants to retrieve his inbox
user1.mailbox.inbox

#user1 wants to get unread messages in inbox
user1.mailbox.inbox(:unread => true)

#user1 wants to get all the read messages in inbox
user1.mailbox.inbox - user1.mailbox.inbox(:unread => true)

#user1 wants to retrieve his sent conversations
user1.mailbox.sentbox

#user1 wants to retrieve his trashed conversations
user1.mailbox.trash

To read the contents of a conversation user get the receipts of the message for him and reads the messages. The following snippet gets the messages in a conversation in reverse chronological order.

#user gets the last conversation (chronologically, the first in the inbox)
conversation = user1.mailbox.inbox.first

#user gets it receipts chronologically ordered.
receipts = conversation.receipts_for user1

#using the receipts (i.e. in the view)
receipts.each do |receipt|
  message = receipt.message
  read = receipt.is_unread?
end

Misc stuff

#To get the originator of the conversation
conversation.originator

#To get all participants of the conversation
conversation.particpants

#To get the first messsage of the conversation
conversation.original_message

#To get no of messages in the conversation
conversation.count_messages

#to mark a conversation as read by the participant (example user2)
user2.mark_as_read(conversation)

To change the views of email messages

#run this command
rails g mailboxer:views

the views are added to your app/views folders so you can edit them.

Thats all for now folks. If you need more info just post in the comments i will try to answer

Update: Follow up tutorial on how to customize mailboxer views

References

  1. https://github.com/ging/mailboxer
  2. http://www.rubydoc.info/github/ging/mailboxer/frames
Categories
ruby on rails

Calling View helper methods from Controller / Model in Rails 3.* and Rails 2.*

To access view helper methods from controller / models in rails 3 you have to use view_context method. Example

view_context.link_to "This is my link", "/"

In rails 2 you have to user @template instance variable as shown below

@template.link_to "This is my link", "/"

Just a note that you should avoid calling these functions within the controllers to follow mvc pattern conventions.