undefined method ‘virtual?’

For weeks I have been avoiding updating my gems because every time I do I get this error:
NoMethodError (undefined method `virtual?' for #<ActiveRecord::ConnectionAdapters::SQLServerColumn:0x7f0436a81628>)
I finally took the time to dig in a figure out why. It appears that the cause of the problem is the new activerecord-oracle_enhanced-adapter gem version 1.4.0. I have heard that this is only a problem if you are connecting to multiple databases. So to fix this issue all we have to do is downgrade this one gem in your Gemfile.
gem 'activerecord-oracle_enhanced-adapter', '1.3.2'
Now bundle install and you’re done!

51 notes 

Using routes in your model in Rails 3.0.x

I am trying to use routes in my model so that I can generate links in a spreadsheet, but I kept getting this error:
RuntimeError (Missing host to link to! Please provide :host parameter or set default_url_options[:host]):

So I started doing some digging and this is what I came up with.

To use routes in your model in Rails 3.0.x you can do this in your model:

class Example < ActiveRecord::Base
  include Rails.application.routes.url_helpers
  Rails.application.routes.default_url_options = ActionMailer::Base.default_url_options
end

This allows you to avoid duplication and allows you to set the default url options in your config environment like this:

ExampleApp::Aplication.configure do
  config.action_mailer.default_url_options = {:host => "example.com"}
end

27 notes 

Generating HTML output file with cucumber in rails 3

My company had a need to be able to run the cucumber tests and have it generate a html file detailing the test status in order to show our customers that we were testing and that they were passing. This does not seem to be very well documented anywhere. So, I went digging through the code and I found that it was actually quite simple.

In the past we were able to do something like this:

cucumber features --format html --out public/output.html

However, as rails has progressed they now recommend that you use “rake cucumber" to run your tests. So how do we pass the other options since "rake cucumber —format html —out public/output.hml" is definitely not going to work.

As it turns out the answer is even more simple and prettier.

When you add cucumber to your project it should generate a rake file (lib/tasks/cucumber.rake). In this file we are going to add our cucumber options to make it generate a html file each time it is run.

t.cucumber_opts = %w{--format html --out public/output.html}

When you add this line your file should look something like this:

  namespace :cucumber do
    Cucumber::Rake::Task.new({:ok => 'db:test:prepare'}, 'Run features that should pass') do |t|
      t.binary = vendored_cucumber_bin # If nil, the gem's binary is used.
      t.cucumber_opts = %w{--format html --out public/output.html} # this is make it output a html file
      t.fork = true # You may get faster startup if you set this to false
      t.profile = 'default'
    end
  end

Now, all you have to do is run “rake cucumber" again and you will have an output.html file in your public directory. It is as simple as that.

4 notes 

I was driving around Albuquerque this morning when I glanced over and there was the best license plate I&#8217;ve ever seen, &#8220;ROR&#8221;, Ruby on Rails suckas!!

I was driving around Albuquerque this morning when I glanced over and there was the best license plate I’ve ever seen, “ROR”, Ruby on Rails suckas!!

1 note 

Simple Calendar Plugin for Ruby on Rails Tutorial

The Simple Calendar Plugin

Created by Slaive and Pullmonkey

With this plugin you can easily add calendar items for any day and then view it in month view or day view.

Install with:

./script/plugin install git://github.com/pullmonkey/simple_calendar.git

Installation

For this pugin you will need several javascript, stylesheet, and image files. These files should be installed automatically on installation. If not then you can use the rake command to install them.

To install these files use:

rake simple_calendar:install

If you want to remove these files then use:

rake simple_calendar:uninstall

Migrations

To generate migrations use:

./script/generate simple_calendar_migrations

This should create 2 migration files:

  • 01_create_simple_calendar.rb
  • 02_create_simple_calendar_entries.rb

Dependencies

This plugin only has 1 dependency which is the acts_as_taggable_on_steroids plugin. This plugin is only needed if you want to be able to make your entries taggable. To install this plugin do:

./script/plugin install http://github.com/jviney/acts_as_taggable_on_steroids.git

Environment.rb

You will need to set your timezone in your environment.rb file. I use the UTC setting so it should look like this:

config.time_zone = 'UTC'

Layout

You only need to add the color stylesheet to your layout. As of now all we have is red and gold. Just pick one and add it to your layout.

<%= stylesheet_link_tag 'simple_calendar_red' %>
<%= stylesheet_link_tag 'simple_calendar_gold' %>

Usage

Put this is your view wherever you want the simple calendar to show up:

<%= render :simple_calendar => "test", :admin => true, :mode => params[:mode], :date => params[:date] %>

You must pass params[:mode] and params[:date] so that after entering an entry the mode and date of where you are will be returned back to the simple calendar controller. This only happens after creating, updating, or removing an entry. So if you don’t plan on the admin to ever be true then you won’t need to pass this at all.

Additional Options

The options you can pass to simple_calendar are:

  • :admin => true/false
    • This decides whether the user can edit the calendar or not
    • defaults to false
  • :writable => true/false
    • This decides whether the user can can add and remove their own items or not. This is useful for a calendar where individual users can schedule their own items, but you don’t want them to mess with other users’ information.
    • Must pass a :user for this to work properly.
    • defaults to false
  • :user => “username"
    • This sets the user that is making changes. A user with :writable set to true can only edit or remove the entries that user created.
    • defaults to “”
  • :prefix => “/test"
    • This is whatever the prefix for your site is. Mainly used for image, stylesheet, javascript, etc. paths.
    • defaults to “”
  • :return_to => “/path/to/my/calendar"
    • This is the path in front of the simple_calendar/
    • defaults to “”
  • :mode => “month"
    • This is the starting mode you want the calendar to be in.
    • defaults to “month
  • :date => “2008-10-29".to_date
    • Passes the starting view date
    • defaults to Date.today
  • :year => 2008
    • If you don’t pass a date then you can pass year seperately
  • :month => 10
    • If you don’t pass a date then you can pass month separately
  • :day => 29
    • If you don’t pass a date then you can pass day separately
  • :show_wiki_entries => true/false
    • When set to true a “Wiki Entries” button will appear. Clicking on this button will pop up a modal with all the entries after the current date in a format easily copied to a Wiki.
    • defaults to false
  • :taggable => true/false
    • Pass true to enable taggable entries. When true you will be able to tag an entry and filter all entries depending on the tag.
    • defaults to false
  • :serial_only=> true/false
    • Pass true to force entries to only be added serially (no overlapping entries).
    • defaults to false
  • :entry_link_length => [integer]
    • The entry name will truncate at this value in the month view.
    • defaults to 15
  • :day_start_hour => [integer]
    • The hour you want to start at in the day view unless there is an entry before this time.
    • Time must be entered in military time (13 is 1pm, 14 is 2pm, etc.)
    • defaults to 0 (which will start at 12am current day)
  • :day_end_hour=> [integer]
    • The hour you want to end at in the day view unless there is an entry for after this time.
    • Time must be entered in military time (13 is 1pm, 14 is 2pm, etc.)
    • defaults to 24 (which will end at 12am the next day)

Small Calendar

This generates a small calendar with the month view and the day numbers are link to the day view in the large simple calendar. To do this put this anywhere in your view:

<%= render :small_simple_calendar => "test", :path => "/path/to/rendering/of/large/calendar" %>

The name of the calendar must be the same as the one you use for the large simple calendar or else the entry data won’t exist. The path is to where you render the simple_calendar.

Upcoming Events

This generates a list of the upcoming events from today’s date on.

<%= render :simple_calendar_upcoming_events => "test", :path => "/path/to/rendering/of/large/calendar" %>

The name of the calendar must be the same as the one you use for the large simple calendar or the entry data won’t exist. The path is to where you render the simple_calendar.

Additional Options

The additional options that you can pas to the simple_calendar_upcoming_events are:

  • :items => 5
    • This is the the max number of items you want listed.
    • default is everything
  • :tag => “mytag"
    • If you are using taggable entries then you can pass a tag or a string of tags (comma delimited) to find from the calendar
    • default is finding everything
  • :entry_name_length => 20
    • The entry name will truncate at this value. This does not include the length of the date.
    • defaults to 100 so pretty much everything

3 notes 

Changes/Fixes to the Simple CMS Plugin

You can also view the tutorial. Or you can check out the demo.

We are continuously adding to and changing the Simple CMS Plugin so here is a little rundown of what has been changed so far:

Major Changes:

  • I have updated to rails 2.0.2 so if you are using an older version you will need to change a line in each of the controllers.
  • 
    
    # Where it has:
      self.view_paths << File.join(File.dirname(__FILE__), '..', 'views')
    
    # Must be Changed to:
      self.template_root = File.join(File.dirname(__FILE__), '..', 'views')
    
    
    
  • The acts_as_versioned plugin is now required for the revisions. I added this quite some time ago. I did change the rake task to install this plugin as well as the rest.
  • You now have to pass a :prefix if you have one. This is to ensure that all content such as images, media, smiles, etc. work correctly. If your base path is a standard path then you do not need to worry about this.
    For example: I was running 3 rails applications under one domain so it looked like this:
    my.domain.com /blog
    /forum
    /demo

    So I would call my Simple CMS content like this for blog:

  • 
    <%= render :simpleCMS => "MyBlog", :admin => true, :prefix => "/blog" %>
    
    
    
  • Now you can make content viewable and editable from multiple pages by passing :reusable => true. This defaults to false. Your label must be the same in each place you use it. This is very useful for layouts, such as header and footer.
    *NOTE* If you already have content here and you change this variable you will lose all that content as it gives the content a new id and changes the params and how the content is called. For example if I have :reusable set to false and I have content there and I set :reusable to true then it creates content with a different id so I will not be able to use the old content until I change it back to false.

Minor Changes:

  • When you click on a revision you are taken back up to the top of the page instead of having to scroll all the way down a list of 100 revisions, clicking on one, and then manually scrolling all the way back up to the top to see if you even picked the right revision
  • I have added code highlighting and this requires the coderay plugin, which has been added to the rake simple_cms:install_dependencies, and a coderay stylesheet so you need to add that to your application layout. I have included it in the rake simple_cms:install
  • 
    <%= stylesheet_link_tag "coderay" %>
    
    
  • I have also changed many things in the javascripts inside the tiny_mce editor so it would be safest just to use the rake tasks that are built in:
  • 
    rake simple_cms:uninstall
    
    rake simple_cms:install
    
    

I have updated the tutorial with all the changes as well.

There is also a demo that you can use to play around with it and ask questions.

1 note 

Simple CMS Plugin for Ruby on Rails Tutorial

The SimpleCMS Plugin

Created by Slaive and PullMonkey (December 2007)

Check out the demo - http://pullmonkey.com/projects/simple_cms

This is still a work in progress so feel free to notify me of any bugs, problems, or suggestions of how to make it better.

This plugin is built for rails 2.0.2. So if you are using an older version of rails then you will need to edit each of the controllers


From:
  self.view_paths << File.join(File.dirname(__FILE__), '..', 'views')


To:
  self.template_root = File.join(File.dirname(__FILE__), '..', 'views')


Imaging Processor

For this plugin to be fully functional you will need to install one of the following Image Processing gems:

  • ImageScience - A light inline-Ruby library that only resizes images.
  • RMagick - The grand-daddy, both in terms of advanced image processing features and memory usage.
  • minimagick - It’s much easier on memory than RMagick because it runs the ImageMagick command in a shell.

Any one of these gems will work.

Install SimpleCMS and Dependencies


script/plugin install git://github.com/pullmonkey/simple_cms.git

The simple_cms plugin requires the attachment_fu, responds_to_parent, acts_as_versioned, and coderay plugins as well. To make this easier I there is a built-in rake process


rake simple_cms:install_dependencies

However if this doesn’t work then you can do it the normal way:



ruby script/plugin install http://svn.pullmonkey.com/plugins/trunk/attachment_fu/
ruby script/plugin install http://svn.pullmonkey.com/plugins/trunk/responds_to_parent/
ruby script/plugin install http://svn.pullmonkey.com/plugins/trunk/acts_as_versioned/
ruby script/plugin install http://svn.pullmonkey.com/plugins/trunk/coderay/

The Javascript/css Files

This plugin requires a great deal of javascript and css files that will need to be copied to the corresponding folder in your public/ directory. These files are located in the simple_cms/assets/ directory.

These files should have been copied over when you install the plugin. However, I built in a couple rake task commands to help you out installing and uninstalling these files if you need to.



rake simple_cms:install
rake simple_cms:uninstall

Creating the Tables

You will need to create 3 tables in your database.

  • simple_cms_items
  • simple_cms_images
  • simple_cms_media

To have the migration tables generated for you use this command:


ruby script/generate simple_cms_migrations

The tables should look like this:

create_simple_cms_items.rb


class CreateSimpleCmsItems < ActiveRecord::Migration

  def self.up
    create_table :simple_cms_items do |t|
      t.column :params,     :string

      t.column :data,       :text
      t.column :position,   :integer
      t.column :created_at, :datetime

      t.column :updated_at, :datetime
      t.column :created_by, :string
      t.column :updated_by, :string

    end
    SimpleCmsItem.create_versioned_table
  end

  def self.down

    SimpleCmsItem.drop_versioned_table
    drop_table :simple_cms_items
  end
end

create_simple_cms_images.rb


class CreateSimpleCmsImages < ActiveRecord::Migration

  def self.up
    create_table :simple_cms_images do |t|
      t.column :parent_id,    :integer

      t.column :content_type, :string
      t.column :filename,     :string
      t.column :thumbnail,    :string

      t.column :size,         :integer
      t.column :width,        :integer
      t.column :height,       :integer

    end
  end

  def self.down
    drop_table :simple_cms_images

  end
end

create_simple_cms_medias.rb


class CreateSimpleCmsMedias < ActiveRecord::Migration
  def self.up

    create_table :simple_cms_medias do |t|
      t.column :parent_id,    :integer
      t.column :content_type, :string

      t.column :filename,     :string
      t.column :thumbnail,    :string
      t.column :size,         :integer

      t.column :width,        :integer
      t.column :height,       :integer
    end
  end


  def self.down
    drop_table :simple_cms_medias
  end
end


There is one more migration file you should have. It’s called change_items_data_colmn and it looks like this:


class ChangeItemsDataColumn < ActiveRecord::Migration
  def self.up

    change_column :simple_cms_items, :data, :text, :limit => 10000000
  end


  def self.down
    change_column :simple_cms_items, :data, :text
  end

end

This is a change to the simple_cms_items table data column. This allows you to store up to 10 megabytes of text instead of the 65 kilobytes it defaulted to.

Remember to rake your tables into your databases once you have generated them.


rake db:migrate


Javascript and css Include Tags

You will need to make sure you have javascript include tags for your defaults and simple_cms and stylesheet link tags for the simple_cms and coderay stylesheets. Your app/views/layouts/application.rhtml should look something like this:


<html>
  <head>
    <%= javascript_include_tag :defaults, "simple_cms" %>

    <%= stylesheet_link_tag "simple_cms", "coderay" %>
  </head>
  <body>
    <%= yield %>

  </body>
</html>

In Your View

Having the simple_cms plugin show up is really pretty simple. Anywhere you want to have the simple_cms to show up you put one line as simple as this:


<%= render :simpleCMS => "YourLabel", :admin => true %>


Here is another example with more options:


<%= render :simpleCMS => "label", :admin => true,

                                  :user => "UserName",
                                  :prefix => "/whatever/your/prefix/is",
                                  :reusable => true %>


  • :simpleCMS takes a label. Changing this label means losing all your current content and creating a new one. However, you can always change it back.
  • :admin takes true or false. The default is false. If you pass true then when you run your mouse over the content a blue highlight box will appear and you will be able to click on the box and edit the content. If you pass false then you will only be able to see the content but not edit any of it.
  • :user is optional and it takes any string you pass it.
  • :prefix is optional. This is where you pass your prefix if you have one.
  • :reusable takes true or false. Default is false. Set this true if you want to use this same content on multiple pages. The label must be the same on all pages you are using this content. *NOTE* you will lose all current data for this content if you change this as it gives the content a different id and changes the params of how it is called.

UNM ECE231 - Spring 2009 - Programming Assignment 4

This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are only going to be doing input and output. So the only header file we need is the iostream library.

Defining the Node Class

For the node class we need 5 private variables: type, value, node1, node2, next, a constructor, 5 member functions, and a friend class.

Because most of the functions only return a single variable it would be a waste to have the functions outside the class so I put them all inside the class.

So here is what your class should look like:

Defining the Circuit Class

For the circuit class we need 2 private variables: first and last, a constructor, a copy constructor, 6 member functions, and a friend function.

For these I will put most of the functions on the outside of the class.

So here is what your class should look like:

Writing the Actual Functions for the Circuit Class

First, we have the private function getNode. This function creates a new member of the node class and returns the address to it.

Second, we have the a constructor, a copy constructor, and a class destructor:

Next, we have the 6 member functions:

Testing your program

Our teacher gave us a test program, however I expanded it a little bit to test a bit more fully.

So here is my test program:

UNM ECE231 - Spring 2009 - Programming Assignment 3

This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are going to be doing a lot of math calculations so we are going to need the include the both the iostream and the cmath libraries. We will also need the value of pi.


#include <iostream>
#include <cmath>
using namespace std;

#define PI 3.141592654
or
const double PI = 3.141592654;

Defining the Complexn Class

For the complexn class we need 2 private variables r and i, 3 constructors, a copy constructor, 4 member functions, 9 overloaded opperators, and 2 friend functions.

Like the previous program we will define everything with in the class and have the actual functions at the bottom.

So here is what your class should look like (keep in mind that this all goes at the top before the main function):

Writing the Actual Functions for the Complexn Class

First we have the 3 constructors and the copy constructor:

Next, we have the 3 member functions:

Next, we have the 9 overloaded operators:

Last, we have the 2 friend functions:

Testing Your program

I wrote a fairly good program for testing this complexn class.

To test your program copy your program to [name].h file and in the main.cpp file change the header file at the top to match your file.

UNM ECE231 - Spring 2009 - Programming Assignment 2

This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are going to be doing a lot of math calculations so we are going to need the include the both the iostream and the cmath libraries. We will also need the value of pi.


#include <iostream>
#include <cmath>

#define PI 3.14159
or
const double PI = 3.14159;

Defining the Coordinate Class

For the coordinate class we need 2 private variables x and y, 3 constructors, and 11 member functions.

The way you do the constructors and member functions can be done 2 different ways. You can either do them inside the class, or you can define them inside the class, like you do prototypes, and then have the actual functions at the bottom of your code. I like to define them and then put the functions at the bottom, making the code a little bit easier to read so that is the way that I’ll show you, but if you want to do it the other way then just do it how you normally would with a regular function.

So here is what your class should look like (keep in mind that this all goes at the top before the main function):


class coordinate
{
private:
double x; // x value of the coordinate point
double y; // y value of the coordinate point
public:
coordinate(); // 1st constructor requiring that no arguments are passed
coordinate(double ); // 2nd constructor requiring that only 1 argument is passed
coordinate(double , double ); // 3rd constructor requiring that 2 arguments are passed

void set(double , double ); // sets both values and requires 2 arguments to be passed
void setx(double ); // sets x value and requires 1 argument to be passed
void sety(double ); // sets y value and requires 1 argument to be passed
void read(); // allows user to input both x and y values
void print(); // prints out the coordinate point in "(x, y)" form
double distancezero(); // calculates distance of point from zero and returns the value
double distancetwo(coordinate ); // calculates distance between 2 points (current instance and coordinate value passed) and returns the value
double ranglezero(); // calculates the angle of the coordinate in radians and returns the value
double danglezero(); // calculates the angle of the coordinate in degrees and returns the value
int quadrant(); // find what quadrant the coordinate is in and returns the value
void midpoint(coordinate ); // calculates the midpoint between 2 points (current instance and coordinate value passed) and prints the value as a coordinate
};

Writing the Actual Functions for the Coordinate Class

Since we are not actually writing the functions in the same place we are doing the class we will write them at the bottom after the main function. However, to do this we will need a little more than what we would with regular functions. For member functions we have to define what class the function is actually a member of. To do this we have to do coordinate::[function]().

For example, here are the 3 constructors for the coordinate class:


// takes no arguments and sets x and y to 0
coordinate::coordinate()
{
x = y = 0;
}
// takes 1 argument and sets x and y to that value
coordinate::coordinate(double a)
{
x = y = a;
}
// takes 2 arguments and sets the corresponding x and y to those values
coordinate::coordinate(double a, double b)
{
x = a;
y = b;
}

Next, we have the member functions. The first 3 are the set functions. These allow the user to set the values of either the x or the y or both together.


// take 2 arguments and sets the corresponding x and y to those values
void coordinate::set(double a, double b)
{
x = a;
y = b;
}
// takes 1 argument and sets the x to that value
void coordinate::setx(double a)
{
x = a;
}
// takes 1 argument and sets the y to that value
void coordinate::sety(double b)
{
y = b;
}

Next, is the read and print functions. All we want the read to do is to do a cin of the 2 values (x and y). The print is just the opposite. All it does is do a cout of the x and y values in the (x, y) format.


// takes no arguments and reads in the x and y values from the screen
void coordinate::read()
{
cin >> x >> y;
}
// takes no arguments and prints the x and y values in (x, y) format
void coordinate::print()
{
cout << "(" << x << ", " << y << ")";
}

Next, we have the distance functions. distanczero calculates the distance of the current instance coordinate from 0. distancetwo calculates the distance between 2 points (the current instance and the coordinate passed in).


// takes no arguments and returns the distance of the current instance from zero
double coordinate::distancezero()
{
return sqrt(x * x + y * y);
}
// take 1 argument and calculates the distance between them
double coordinate::distancetwo(coordinate pt)
{
return sqrt(pow(x - pt.x, 2.0) + pow(y - pt.y, 2.0));
}

Next, we have the angle functions. ranglezero uses the function in the math library atan()(arc tangent) to find the angle in radians from the positive x axis. danglezero does the same thing but converts the value from radians to degrees. To convert from radians to degrees you multiply the value by 180 / pi


// takes no arguments and returns the angle of the coordinate from the positive x axis in radians
double coordinate::ranglezero()
{
double angle; // angle of coordinate from positive x axis
if(x == 0) // if x == 0 then you will get a domain error so compute angle manually
{
if(y > 0) return PI / 2;
if(y < 0) return -PI / 2;
if(y == 0) return 0;
}
angle = atan(y / x);
if(x < 0 and y >= 0)
return PI - abs(angle);
if(x < 0 and y < 0)
return -(PI - abs(angle));
return angle;
}
// takes no arguments and returns the angle of the coordinate from the positive x axis in degrees
double coordinate::danglezero()
{
double angle; // angle of coordinate from positive x axis
if(x == 0) // if x == 0 then you will get a domain error so compute angle manually
{
if(y > 0) return 90;
if(y < 0) return -90;
if(y == 0) return 0;
}
angle = atan(y / x) * 180 / PI;
if(x < 0 and y >= 0)
return 180 - abs(angle);
if(x < 0 and y < 0)
return -(180 - abs(angle));
return angle;
}

Next, we have the quadrant function. This looks at whether the x and y values are positive or negative to determine which quadrant the coordinate is in. It will return the number of the quadrant as an integer 1-4 and 0 if the coordinate is (0, 0)


int coordinate::quadrant()
{
if(x > 0 && y >= 0)
return 1; // return quadrant 1
if(x <= 0 && y > 0)
return 2; // return quadrant 2
if(x < 0 && y <= 0)
return 3; // return quadrant 3
if(x >= 0 && y < 0)
return 4; // return quadrant 4
return 0; // return 0 if point is (0, 0)
}

Last, we have the midpoint function. you will need to pass this function a coordinate instance and it will calculate the midpoint between the current instance and the passed instance and print the coordinate instance.


void coordinate::midpoint(coordinate pt)
{
coordinate np; // new coordinate point
np.x = (x + pt.x) / 2.0;
np.y = (y + pt.y) / 2.0;
np.print();
}

Now all you have to do is test each constructor and member function to make sure that everything is working properly.