-
- Using Presenters in Rails
-
Sitting thrugh my first tutorials session at RailsConf we’ve come across an interesting discussion about the philosophy of using the ‘Presenter Pattern’ to refactor code in your applications controller.
-
- Rails: What’s the best way to handle my lists?
-
Ok so I’m having a slight problem building my CMS for a client. I want to utilize the same model to handle the photos for their case studies as well as their services. Each has their own model. So the Photo model belongs to a Project or a Service. But in reality it belongs to a project OR a service. Thus I have a problem using acts_as_list.
class Photo < ActiveRecord::Base belongs_to :service belongs_to :project acts_as_taggable acts_as_list :scope => :project_id file_column :image, :magick => { :versions => { "thumb" => "50x50", "medium" => "640x480>" } } endIf I could do something like:
acts_as_list :scope => :project_id || :service_id
Then this would be a piece of cake but it’s not that easy. How would you solve this? I know it’s not that challenging.
-
- Creating Excel Documents with Ruby On Rails
-
Generating an excel document is not so difficult to achieve but limited unfortunately. You cannot write formulas or generate charts via the existing available tools but you can achieve usable results overall. Here is a snippet I used to generate sales forecasts involving data pulled together from two active record objects. Here is the gem you’ll need.
- Update: the link I was posting to prior points to a new Ruby project. The correct link can be found here.
# Builds an excel report. def report # Grab time span for report get_span # Define stats levels to include. status = %w(high medium low lost won) # Create workbook. file = "#{session[:user_id]}_#{Time.now.strftime("%m%d%G%s")}_forecast.xls" workbook = Excel.new("#{RAILS_ROOT}/reports/#{file}") heading = Format.new( :color => "green", :bold => true, :underline => true ) data = Format.new( :color => "black", :bold => false, :underline => false ) workbook.add_format(heading) workbook.add_format(data) # Cycle through each status level status.each do |status| start_column, start_row = 2, 3 worksheet = workbook.add_worksheet(status) opportunities = get_opportunities_that_are(status) #Cycle through the opportunities row = start_row totals, dates = [], [] for opp in opportunities worksheet.write(row,start_column,opp.client,heading) column = start_column + 1 opp.find_forecasts_within(@span[0],@span[-1]).each do |i| worksheet.write(row,column,i.volume,data) totals[column] = i.volume + totals[column].to_i dates[column] = i.date.strftime("%b '%y") column += 1 end row += 1 end # Generate the totals row and monthly headings column = start_column+1 @span.length.times do worksheet.write(row,column,totals[column],heading) worksheet.write(start_row-1,column,dates[column],heading) column += 1 end end workbook.close redirect_to :action => 'show' end
Tagged rails
- I’m on Rails: Coding Statistics.
-
I recently coded a basic statistics applicaiton for a client that offers a website affiliate program. I thought it might be worth sharing how easy it is to code this type of software in Rails. Keep in mind this is extremely basic but never the less a good illustration of how to tackle a cumbersome project relatively easily.
Decide what to track.
For this client all we needed to track were the number of actual page impressions, the amount of clicks, and the actual amount of unique visitors going to a specific users account.
Playing Big Brother.
Watching visitor activity and logging it is easy. I created a model for hits, clicks, and visits. There is a parent model to all of these called ‘User’. So here’s how I broke down the relationships:
- Hits belong to users.
- Visits belong to users.
- Clicks belong to both users and visitors.
Below is the code that logs the click through information and sends the user off on their happy way. It basically takes an id and a path so a typical link would look like: http://www.domain.com/send_to/id/encoded_url_path_to_redirect_to
def send_to if @user = User.find(:first, :conditions => ["name=?",@params[:id].to_s.upcase]) # Get parameter info... url = params[:path].to_s ip = request.remote_ip # Log the click information. click = Click.new click.url = url @user.clicks < < click # Log the visitor information. visitor = Visitor.create_if_new_today(ip,@user.id) visitor.clicks < < click # Log the hit. hit = Hit.new @user.hits < < hit if @user.save redirect_to url end endIt should be worth noting that I’ve decided to track where every click is redirecting too. I’ve also decided to track every click as a hit for the user as well as technically it is an impression.
Logging unique visitor information as well as the page impression itself (the hit) is easy too as seen below.
def index if params[:id] == 'nonexistent' params[:id] = 'admin' end if @user = User.find(:first, :conditions => ["name=?",params[:id].to_s.upcase]) # Log the hit. hit = Hit.new @user.hits < < hit # Log the visitor information. ip = request.remote_ip visitor = Visitor.create_if_new_today(ip,@user.id) @user.visitors < < visitor @time = Time.now @user_id = params[:id].to_s.upcase else redirect_to(:controller => 'show', :id =>'nonexistent') end endGrabbing the stats.
Once you’ve logged the stats pulling them via rails is a piece of cake. I wrote a protected function inside of my report controller to pull the hits, clicks, and visits for a specific time length and return them in a hash.
# Display user stats. def report @time = Time.now @user = User.find(session[:user_id]) @todays = stats(@user,1.days.ago) @last_weeks = stats(@user,1.weeks.ago) @last_months = stats(@user,4.weeks.ago) @records = transactions end protected # Generates a hash containing hits, visitors, and clicks from the past. def stats(user,time) clicks = user.clicks.find(:all, :conditions => ["created_on > ?", time]) hits = user.hits.find(:all, :conditions => ["created_on > ?", time]) visitors = user.visitors.find(:all, :conditions => ["created_on > ?", time]) return { :hits => hits, :visitors => visitors, :clicks => clicks } end
- Easy File Uploads with Ruby on Rails.
-
This is nifty… check out this file_column behavior for Ruby on Rails. Wow that looks damn easy.
- Ruby On Rails 1.0 Is Here!
-
Today “Ruby on Rails 1.0″:http://www.rubyonrails.org/ was finally released. There were two final release candidates that popped up over the last two weeks so I’m not surprised to see it here today. But what is surprising is the entire redesign of the Rails website. Check out “David’s post”:http://weblog.rubyonrails.org/articles/2005/12/13/rails-1-0-party-like-its-one-oh-oh to learn more about 1.0!
If you’re interested in starting up a Rails application of your own I recommend looking into grabbing an account at “Site5″:http://www.site5.com, “DreamHost”:http://www.dreamhost.com, or “TextDrive”:http://www.textdrive.com as they all offer stable shared hosting environments for Rails and should be upgrading their servers to 1.0 in the next couple of days if they haven’t already!