Using Disqus and Ruby on Rails
Recently, I posted about how to import comments from a Ruby on Rails app to Disqus. This is a follow up to that post where I outline the implementation of Disqus in a Ruby on Rails site. Disqus provides what it calls Universal Code which can be added to any site. This universal code is just JavaScript, which asynchronously loads the Disqus thread based on one of two unique identifiers Disqus uses.
Disqus in a development environment
Before we get started, I’d recommend that you have two Disqus “sites”; one for development and one for production. This will allow you to see real content and experiment with how things will really behave once you’re in production. Ideally, your development server would be publicly accessible to allow you to fully use the Disqus moderation interface, but it isn’t required. Simply register another Disqus site, and make sure that you have your shortname configured by environment. Feel free to use whatever method you prefer for defining these kinds of application preferences. If you’re looking for an easy way, considering checking out my article on Working with Constants in Ruby. It might look something like this:
# app/models/article.rb
DISQUS_SHORTNAME …
javascript rails
ActiveRecord Callbacks for Order Processing in Ecommerce Applications
As I recently blogged about, I introduced a new Ruby on Rails Ecommerce Engine. The gem relies on RailsAdmin, a Ruby on Rails engine that provides a nice interface for managing data. Because the RailsAdmin gem drives order creation on the backend in the context of a standard but configurable CRUD interface, and because I didn’t want to hack at the RailsAdmin controllers, much of the order processing logic leverages ActiveRecord callbacks for processing. In this blog article, I’ll cover the process that happens when an order is saved.
Order Data Model
The first thing to note is the data model and the use of nested attributes. Here’s how the order model relates to its associated models:
class Order < ActiveRecord::Base
has_many :line_items, :inverse_of => :order
has_many :payments, :inverse_of => :order
has_many :shipments, :inverse_of => :order
has_many :credits, :inverse_of => :order
belongs_to :billing_address, :class_name => "Piggybak::Address"
belongs_to :shipping_address, :class_name => "Piggybak::Address"
belongs_to :user
accepts_nested_attributes_for :billing_address, :allow_destroy => true …
ecommerce open-source piggybak rails
Interchange loops using DBI Slice
One day I was reading through the documentation on search.cpan.org for the DBI module and ran across an attribute that you can use with selectall_arrayref() that creates the proper data structure to be used with Interchange’s object.mv_results loop attribute. The attribute is called Slice which causes selectall_arrayref() to return an array of hashrefs instead of an array of arrays. To use this you have to be working in global Perl modules as Safe.pm will not let you use the selectall_arrayref() method.
An example of what you could use this for is an easy way to generate a list of items in the same category. Inside the module, you would do like this:
my $results = $dbh->selectall_arrayref(
q{
SELECT
sku,
description,
price,
thumb,
category,
prod_group
FROM
products
WHERE
category = ?},
{ Slice => {} },
$category
);
$::Tag->tmpn("product_list", $results);
In the actual HTML page, you would do this:
<table cellpadding=0 cellspacing=2 border=1>
<tr>
<th>Image</th>
<th>Description</th>
<th>Product Group</th>
<th>Category</th> …
database interchange perl
Take a snapshot in Cucumber and sync it with Dropbox!
In a previous post I talked about running cucumber using capybara-webkit. In a recent project using this setup I noticed that I couldn’t use capybara in connection with launchy to open up a page in the browser for debugging tests. The “save and open page” step is one that I used a lot when I was developing locally. But now that I’m developing on a server, I don’t have any way save the page or open it for review.
The solution I found to this comes in two parts. First, create a “take a snapshot” cucumber step that drops a snapshot of the HTML and a PNG of the page in a temp directory. Second, add that temp directory to dropbox so that it gets synced to my desktop automatically when it is created.
Wait, seriously? Dropbox?
Yes, absolutely. Dropbox.
I often develop inside of my dropbox folder because A) all my code is automatically backed up, even with versions and B) because it’s really simple to sync my code to other computers. I’ll admit that one problem I had early on was that log files were using an awful amount of bandwidth getting copied up constantly, but I solved this by adding the log directory to an exclusions list. I’ll show you how to do that below.
Step 1: Create a “take …
rails
Introducing Piggybak: A Mountable Ruby on Rails Ecommerce Engine
Here at End Point, we work with a variety of open source solutions, both full-fledged ecommerce applications and smaller modular tools. In our work with open source ecommerce applications, we spend a significant amount of time building out custom features, whether that means developing custom shipping calculations, product personalization, accounting integration or custom inventory management.
There are advantages to working with a full-featured ecommerce platform. If you are starting from scratch, a full-featured platform can be a tool to quickly get product out the door and money in your pocket. But to do this, you must accept the assumptions that the application makes. And most generic, monolithic ecommerce platforms are created to satisfy many users.
Working with a large monolithic ecommerce platform has disadvantages, too:
- Sometimes over-generic-izing a platform to satisfy the needs of many comes at the cost of code complexity, performance, or difficulty in customization.
- Occasionally, the marketing of an ecommerce solution overpromises and underdelivers, leaving users with unrealistic expectations on what they get out of the box.
- Customization on any of these platforms is …
ecommerce piggybak rails
Ruby on Rails: Attributes Management through Rights
I’ve written a couple times about a large Rails project that a few End Pointers have been working on. The system has a fairly robust system for managing rights and access. The basic data model for rights, users, groups, and roles is this:

In this data model, right_assignments belong to a single right, and belong to a subject, through a polymorphic relationship. The subject can be a Role, Group, or User. A User can have a role or belong to a group. This allows for the ability to assign a right to a user directly or through a group or role.
In our code, there is a simple method for grabbing a user’s set of rights:
class User < ActiveRecord::Base
...
def all_rights
rights = [self.rights +
self.groups.collect { |g| g.allowed_rights } +
self.roles.collect { |r| r.rights }]
rights = rights.flatten.uniq.collect { |r| r.action }
rights
end
...
end
In the case of this data model, groups also have a boolean which specifies whether or not rights can be assigned to them. The allowed_rights method looks like this:
class Group < ActiveRecord::Base
...
def allowed_rights
self.assignable_rights ? self …
rails
Some great press for College District
College District has been getting some positive press lately, the most recent being a Forbes article which talks about the success they have been seeing in the last few years.
College District is a company that sells collegiate merchandise to fans. They got their start focusing on the LSU Tigers at TigerDistrict.com and have branched out to teams such as the Oregon Ducks and Alabama Roll Tide.
We’ve been working with Jared Loftus at College District for over four years. College District is running on a heavily modified Interchange system with some cool Postgres tricks. The system can support a nearly unlimited number of sites, running on 2 catalogs (1 for the admin, 1 for the front end) and 1 database. The key to the system is different schemas, fronted by views, that hide and expose records based on the database user that is connected. The great thing about this system is that Jared can choose to launch a new store within a day and be ready for sales, something he has taken advantage of in the past when a team is on fire and he sees an opportunity he can’t pass up.
We are currently preparing for a re-launch of the College District site that will focus on crowd-sourced designs. …
database ecommerce hosting interchange postgres clients
Automating removal of SSH key patterns
Every now and again, it becomes necessary to remove a user’s SSH key from a system. At End Point, we’ll often allow multiple developers into multiple user accounts, so cleaning up these keys can be cumbersome. I decided to write a shell script to brush up on those skills, make sure I completed my task comprehensively, and automate future work.
Initial Design and Dependencies
My plan for this script is to accept a single argument which would be used to search the system’s authorized_keys files. If the pattern was found, it would offer you the opportunity to delete the line of the file on which the pattern was found.
I’ve always found mlocate to be very helpful; it makes finding files extremely fast and its usage is trivial. For this script, we’ll use the output from locate to find all authorized_keys files in the system. Of course, we’ll want to make sure that the mlocate.db has recently been updated. So let’s show the user when the database was last updated and offer them a chance to update it.
mlocate_path="/var/lib/mlocate/mlocate.db"
if [ -r $mlocate_path ]
then
echo -n "mlocate database last updated: "
stat -c %y $mlocate_path
echo -n "Do you …
hosting tools