Ruby on Rails - Multiple database connections
April 21st, 2008 by charlie
So, let's say that we want to use two databases and let's even say that we want to use an Oracle database and a MySQL database. How can this be done? To start, we must decide which database will be our default database. In this scenario, I chose MySQL. Let's see what that looks like:
1 2 3 4 5 6 7 |
# in your database.yml file
development:
adapter: mysql
username: root
password:
database: example_development
|
But, I need to use data from an oracle database, so let's setup that connection:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# in your database.yml file
development:
adapter: mysql
username: root
password:
database: example_development
oracle_development:
adapter: oracle
username: root
password:
database: example_oracle_development
|
1 2 3 4 |
#RAILSROOT/app/models/user.rb class User < ActiveRecord::Base end |
1 2 |
establish_connection :oracle_development
|
1 2 3 |
# use RAILS_ENV where RAILS_ENV is generally development, test or production establish_connection "oracle_#{RAILS_ENV}" |
database.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
dev_basics: &dev_basics username: root password: <% %w(development test production).each do |env| %> <%= env %>: <<: *dev_basics adapter: mysql database: example_<%= env %> oracle_<%= env %>: <<: *dev_basics adapter: oracle database: example_oracle_<%= env %> <% end %> |
Oracle model example
1 2 3 4 |
class User < ActiveRecord::Base establish_connection "oracle_#{RAILS_ENV}" end |
That should be it.
Open Flash Chart Graphing for Ruby on Rails - Updates
April 5th, 2008 by charlie
Thanks go to Brandon, who provided an example of using javascript as the output type:
View Source Code
So if this was affecting you, then you will want to get the latest version of the plugin - Instructions.
Ruby - include? versus intersection
April 2nd, 2008 by charlie
I decided to see if I could write a faster include?() method for Array using the intersection operator. So here is what I did:
1) Tested via irb (of course)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[pullmonkey]$ irb # build the array for conceptual testing irb(main):003:0> a = [1,2,3] => [1, 2, 3] # see how include?() does it irb(main):004:0> a.include?(2) => true irb(main):005:0> a.include?(4) => false # see what could be done with intersect irb(main):006:0> a & [2] => [2] irb(main):007:0> (a & [2]).empty? => false # this should return true, so negate it irb(main):008:0> !(a & [2]).empty? => true irb(main):009:0> !(a & [4]).empty? => false |
2) Extended the Array class with the faster_include?() method:
1 2 3 4 5 6 |
class Array def faster_include?(n) !(self & [n]).empty? end end |
3) Wrote a performance test comparing elapsed time for true and false results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# size of the array x = 4000000 # build the array a = (1..x).to_a p "Finding #{x/2} ..." t1 = Time.now p "include?() - returns true: #{a.include?(x/2)}" t2 = Time.now p "faster_include?() - returns true: #{a.faster_include?(x/2)}" t3 = Time.now p "include?() - returns false: #{a.include?(-50)}" t4 = Time.now p "faster_include?() - returns false: #{a.faster_include?(-50)}" t5 = Time.now p "Elapsed time for include?() returning true: #{t2 - t1}." p "Elapsed time for faster_include?() returning true: #{t3 - t2}." p "Elapsed time for include?() returning false: #{t4 - t3}." p "Elapsed time for faster_include?() returning false: #{t5 - t4}." |
4) Just to see if there was a difference I got results and here they are:
Attempt 1:1 2 3 4 5 6 7 8 9 10 |
"Finding 2000000 ..." "include?() - returns true: true" "faster_include?() - returns true: true" "include?() - returns false: false" "faster_include?() - returns false: false" "Elapsed time for include?() returning true: 0.308243." "Elapsed time for faster_include?() returning true: 0.271465." "Elapsed time for include?() returning false: 0.629375." "Elapsed time for faster_include?() returning false: 0.242673." |
1 2 3 4 5 6 7 8 9 10 |
"Finding 2000000 ..." "include?() - returns true: true" "faster_include?() - returns true: true" "include?() - returns false: false" "faster_include?() - returns false: false" "Elapsed time for include?() returning true: 0.323652." "Elapsed time for faster_include?() returning true: 0.272212." "Elapsed time for include?() returning false: 0.594751." "Elapsed time for faster_include?() returning false: 0.277882." |
5) Better testing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
results = {}
n = 5
# size of the array
[1,50,500,5000,5000000].each do |x|
a = (1..x).to_a
n.times do |t|
# build the array
t1 = Time.now
a.include?(x/2)
t2 = Time.now
a.faster_include?(x/2)
t3 = Time.now
a.include?(-50)
t4 = Time.now
a.faster_include?(-50)
t5 = Time.now
results[x] ||= {}
results[x]["true"] ||= []
results[x]["false"] ||= []
results[x]["true"] << (t2 - t1) - (t3 - t2)
results[x]["false"] << (t4 - t3) - (t5 - t4)
end
detailed_results = results
results[x] = {"true" => detailed_results[x]["true"].sum / n, "false" => detailed_results[x]["false"].sum / n}
end
pp results
|
My new results:
The results are based on 5 attempts for each array for both true and false for a total of 50 attempts.1 2 3 4 5 6 |
{5000000=>{"true"=>0.0921126, "false"=>0.4374644},
5000=>{"true"=>5.84e-05, "false"=>0.0004404},
500=>{"true"=>1.4e-06, "false"=>4.16e-05},
50=>{"true"=>-3.4e-06, "false"=>1.0e-06},
1=>{"true"=>-4.8e-06, "false"=>-3.8e-06}}
|
So there you have it, faster_include?() can keep its name :)
Intersection of Array of Arrays - Ruby
March 31st, 2008 by charlie
Let's say that you have a has and belongs to many (habtm) association between articles and tags, (I.e., a tag habtm articles and an article habtm tags). This means that tag.articles will return a list of articles that have that tag and article.tags will return the tags of the article. So if a user wants to search your blog for all articles that have 'tag1' and 'tag2', you would return all common articles.
Picture a search page with a list of check boxes, one for each tag that you have. The user can click as many tags as they want and then it is your job to find the common articles between them. The search POST contains the ids of the selected tags, like params[:tag_ids] = ["1", "3", "15"] or something. Let's go with this example.
1) The first step is to find those tags:
1 |
>> tags = Tag.find(params[:tag_ids]) |
1 2 |
>> tags.map(&:article_ids) => [[1,2,3],[2,3,4,5],[2,3,6]] |
3) Turn each sub array into a string like "[1,2,3]"
1 2 |
>> tags.map(&:article_ids).map(&:to_json) => ["[1, 2, 3]", "[2, 3, 4, 5]", "[2, 3, 6]"] |
1 2 |
>> tags.map(&:article_ids).map(&:to_json).join("&") => "[1, 2, 3]&[2, 3, 4, 5]&[2, 3, 6]" |
1 2 |
>> eval(tags.map(&:article_ids).map(&:to_json).join("&")) => [2,3] |
So a one-liner would be:
1 2 |
>> Article.find(eval(Tag.find(params[:tag_ids]).map(&:article_ids).map(&:to_json).join("&"))) => <returns an array of article objects> |
Dynamic Select Boxes - Ruby on Rails
March 30th, 2008 by charlie
For this tutorial I am going to have three select boxes. The first select box will be a super category of the next two select boxes and the second select box will be a super category of the third select box. I hope that makes sense. To demonstrate, I thought I would use Genre -> Artist -> Song. So let's get started:
Create your models and build your migrations:
1 2 3 4 |
ruby script/generate model genre name:string ruby script/generate model artist name:string genre_id:integer ruby script/generate model song title:string artist_id:integer |
1 2 |
ruby script/generate migration create_hierarchy |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class CreateHierarchy < ActiveRecord::Migration def self.up g1 = Genre.create(:name => "Genre 1") g2 = Genre.create(:name => "Genre 2") g3 = Genre.create(:name => "Genre 3") a1 = Artist.create(:name => "Artist 1", :genre_id => g1.id) a2 = Artist.create(:name => "Artist 2", :genre_id => g1.id) a3 = Artist.create(:name => "Artist 3", :genre_id => g2.id) a4 = Artist.create(:name => "Artist 4", :genre_id => g2.id) a5 = Artist.create(:name => "Artist 5", :genre_id => g3.id) a6 = Artist.create(:name => "Artist 6", :genre_id => g3.id) Song.create(:title => "Song 1", :artist_id => a1.id) Song.create(:title => "Song 2", :artist_id => a1.id) Song.create(:title => "Song 3", :artist_id => a2.id) Song.create(:title => "Song 4", :artist_id => a2.id) Song.create(:title => "Song 5", :artist_id => a3.id) Song.create(:title => "Song 6", :artist_id => a3.id) Song.create(:title => "Song 7", :artist_id => a4.id) Song.create(:title => "Song 8", :artist_id => a4.id) Song.create(:title => "Song 9", :artist_id => a5.id) Song.create(:title => "Song 10", :artist_id => a5.id) Song.create(:title => "Song 11", :artist_id => a6.id) Song.create(:title => "Song 12", :artist_id => a6.id) end def self.down # you can fill this in if you want. end end |
1 2 |
rake db:migrate
|
1 2 3 4 5 6 7 8 |
class Genre < ActiveRecord::Base has_many :artists has_many :songs, :through => :artists end class Artist < ActiveRecord::Base has_many :songs end |
1 2 3 4 5 6 7 8 9 10 |
ruby script/console Loading development environment (Rails 2.0.2) >> g = Genre.find(:first) => #<Genre id: 1, name: "Genre 1", created_at: "2008-03-30 11:52:25", updated_at: "2008-03-30 11:52:25"> >> g.artists => [#<Artist id: 1, name: "Artist 1", genre_id: 1, created_at: "2008-03-30 11:52:25", updated_at: "2008-03-30 11:52:25">, #<Artist id: 2, name: "Artist 2", genre_id: 1, created_at: "2008-03-30 11:52:25", updated_at: "2008-03-30 11:52:25">] >> g.songs => [#<Song id: 1, title: "Song 1", artist_id: 1, created_at: "2008-03-30 11:52:25", updated_at: "2008-03-30 11:52:25">, #<Song id: 2, title: "Song 2", artist_id: 1, created_at: "2008-03-30 11:52:25", updated_at: "2008-03-30 11:52:25">, #<Song id: 3, title: "Song 3", artist_id: 2, created_at: "2008-03-30 11:52:25", updated_at: "2008-03-30 11:52:25">, #<Song id: 4, title: "Song 4", artist_id: 2, created_at: "2008-03-30 11:52:25", updated_at: "2008-03-30 11:52:25">] >> |
# the controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class TestItController < ApplicationController def index @genres = Genre.find(:all) @artists = Artist.find(:all) @songs = Song.find(:all) end def update_artists # updates artists and songs based on genre selected genre = Genre.find(params[:genre_id]) artists = genre.artists songs = genre.songs render :update do |page| page.replace_html 'artists', :partial => 'artists', :object => artists page.replace_html 'songs', :partial => 'songs', :object => songs end end def update_songs # updates songs based on artist selected artist = Artist.find(params[:artist_id]) songs = artist.songs render :update do |page| page.replace_html 'songs', :partial => 'songs', :object => songs end end end |
# the _songs partial (_songs.html.erb):
1 2 3 |
<%= collection_select(nil, :song_id, songs, :id, :title, {:prompt => "Select a Song"}) %> |
1 2 3 4 5 6 |
<%= collection_select(nil, :artist_id, artists, :id, :name, {:prompt => "Select an Artist"}, {:onchange => "#{remote_function(:url => {:action => "update_songs"}, :with => "'artist_id='+value")}"}) %> <br/> |
1 2 3 4 5 6 7 8 9 |
<%= javascript_include_tag :defaults %> <%= collection_select(nil, :genre_id, @genres, :id, :name, {:prompt => "Select a Genre"}, {:onchange => "#{remote_function(:url => {:action => "update_artists"}, :with => "'genre_id='+value")}"}) %> <br/> <div id="artists"><%= render :partial => 'artists', :object => @artists %></div> <div id="songs"><%= render :partial => 'songs', :object => @songs %></div> |
For now, if you have any questions, just ask me in the comments.
Changes/Fixes to the Simple CMS Plugin
February 28th, 2008 by slaive
You can also click here for the tutorial. Or you can click here to 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.
- 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:
So I would call my Simple CMS content like this for blog:my.domain.com /blog /forum /demo - 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.
1 2 3 4 5 6 |
# 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') |
1 2 3 |
<%= render :simpleCMS => "MyBlog", :admin => true, :prefix => "/blog" %>
|
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
- 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:
1 2 3 |
<%= stylesheet_link_tag "coderay" %>
|
1 2 3 4 |
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.
Errno::EINVAL - Invalid argument
February 19th, 2008 by charlie
The typical error you get looks something like this:
Errno::EINVAL in .....
Invalid argument....
.../lib/active_record/base.rb:1358:in `write'
.../lib/active_record/base.rb:1358:in `compute_type'
....
Ya, that is not something you want to get and have no idea what is going on, not to mention that it only happens every once in a while. Especially, when you are already frustrated because nothing else seems to work perfectly.
The error is caused when you are using a windows server and have decided that, as a good windows' administrator, you will create a mongrel service for your application and set it to start automatically. The service knows nothing about STDOUT, so anything written to STDOUT will produce the error above. But, what still baffles me is the complete and utter randomness of the whole situation. Why does it not happen all the time, why only every 6 to 10 times that I hit the same exact page? Well, I can't help you there, but let me know if you have figured it out.
So, if you are lucky enough to get the same error above, then I have a solution for you, well actually a couple solutions. First, if you have the choice, ditch windows, not worth it. However, if you are like me and are forced to use windows for a project then your solution is a simple derivative of what you will find here.
None of those solutions worked for me, or at least not to the point where I was satisfied. They all got me very close, but without a way of still logging STDOUT. The closest and, as the link above points out, the safest was to redirect STDOUT to a logfile:
1 2 |
STDOUT.reopen(...)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
################################### # RAILS_ROOT/lib/std_out_logger ################################### class StdOutLogger def write(s) f = File.open("log/stdout.log", "w+") f.puts s.inspect f.close end end ################################### # RAILS_ROOT/app/controllers/application.rb ################################### # redirect the output to a log file for when we run as a service before_filter { $stdout = $stderr = StdOutLogger.new } |
I hope that helps you, it worked for me. Good luck.
Open Flash Chart Plugin for Ruby on Rails
February 4th, 2008 by charlie
Check out what I can do now :) I can customize my tooltips:
Extra Tool Tips
View Source Code
More examples can be found on my Open Flash Chart Projects page.
Don't forget to check out the original - http://teethgrinder.co.uk/open-flash-chart/.
Simple CMS Plugin for Rails - with revisions
February 2nd, 2008 by charlie
Check out the Simple CMS Plugin Demo
On this page you will see the "Show all revisions" link at the bottom of the "edit" page.
So feel free to add or delete or change revisions at will.
Also, if you are new to SimpleCMS, check out the sort of rough getting stared page.
Well, enjoy and please leave comments/feedback/requests here in the comments or preferably in the list of requests we have started on the demo page :)
Rounding to the nearest number in Ruby
January 31st, 2008 by charlie
1 2 3 4 5 6 7 8 |
class Fixnum def roundup return self if self % 10 == 0 # already a factor of 10 return self + 10 - (self % 10) # go to nearest factor 10 end end |
- Use the Numeric class (this class encompasses Float, Fixnum and Integer)
- Don't limit the method to just the nearest 10, have it as a parameter
- And an added bonus - rounddown()
Here is the resulting code (defaulting to 10):
1 2 3 4 5 6 7 8 9 10 |
class Numeric def roundup(nearest=10) self % nearest == 0 ? self : self + nearest - (self % nearest) end def rounddown(nearest=10) self % nearest == 0 ? self : self - (self % nearest) end end |
1 2 3 4 5 6 7 8 9 |
puts 2.roundup #=> 10 puts 23.roundup #=> 30 puts 20.roundup #=> 20 puts 45.roundup #=> 50 puts 156.roundup #=> 160 puts 156.34.roundup #=> 160.0 puts 16.34.roundup #=> 20.0 puts 81.1234.roundup #=> 90.0 |
Convert a Ruby hash into a class object
January 5th, 2008 by charlie
In the post, the user wanted to load a YAML object into his hash and then present the data from the hash in a form. Needless to say it was not very DRY the way it had to be implemented. So I started looking into it, I found this. This solution was a great starting point for where I ended up, but it was not general enough, it was hard coded, plus it was missing the getters and setters. So it turns out that in ruby it wasn't too much trouble to convert a hash into a class object. So let's get started:
I have implemented this for use in Rails, so let's start with the model that does all the magic:
1 2 3 4 5 6 7 8 9 10 11 |
class Hashit def initialize(hash) hash.each do |k,v| self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable end end end |
Ok, so on to the Controller that creates the Hashit object:
1 2 3 4 5 6 7 8 9 |
class TestItController < ApplicationController def index hashit = {:support_email => "test@test.com", :allow_comments => 0} @hashit = Hashit.new(hashit) end end |
1 2 |
#<Hashit:0xb6a65110 @allow_comments=0, @support_email="test@test.com">
|
Note: In this example @hashit is an actual hash, not a class object.
1 2 3 4 5 6 |
<% form_tag :action => 'config', :method => :post do %> <%= text_field 'settings', 'support_email', :size => 20, :value => @hashish['support_email']%> ... <% end %> |
1 2 3 4 5 |
<% form_for :hashit, :url => {:action => 'index'} do |f| %> <%= f.text_field :support_email %> <% end %> |
Well that is pretty much it, I suppose the next step would be to have a save method that updates the hash? This way we can do @hashit.save() and it will return a new hash that you can use. Well actually, that probably isn't too hard, lets see if I can do it real quick, class object back to hash ...
Well, I am back and I was able to figure it out, here is the new class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Hashit def initialize(hash) hash.each do |k,v| self.instance_variable_set("@#{k}", v) self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) end end def save hash_to_return = {} self.instance_variables.each do |var| hash_to_return[var.gsub("@","")] = self.instance_variable_get(var) end return hash_to_return end end |
1 2 |
{"support_email"=>"new@some_email", "allow_comments"=>0}
|
OpenGL in the browser
January 5th, 2008 by charlie
Check out this guy's opengl stuff, really cool!
This requires java applet support, and I can say that it works wonderfully in Linux/FireFox, and Alias (from above) says that it works with IE. So there you have it.
Use your mouse and move it around ....
Built with Processing
Open Flash Chart Plugin for Ruby on Rails
January 3rd, 2008 by charlie
Check out what I can do now :) Like the google analytics graphs.
Scatter Chart
View Source Code
More examples can be found on my Open Flash Chart Projects page.
Don't forget to check out the original - http://teethgrinder.co.uk/open-flash-chart/.
Simple CMS Plugin for Rails Demo
December 29th, 2007 by charlie
Please let us know what works, what does not work.
Thank you.
Simple CMS Plugin for Ruby on Rails Tutorial
December 23rd, 2007 by slaive
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
1 2 3 4 5 6 7 |
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
1 2 3 |
ruby script/plugin install http://svn.pullmonkey.com/plugins/trunk/simple_cms/ |
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
1 2 3 |
rake simple_cms:install_dependencies |
However if this doesn't work then you can do it the normal way:
1 2 3 4 5 6 |
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.
1 2 3 4 |
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:
1 2 3 |
ruby script/generate simple_cms_migrations |
The tables should look like this:
create_simple_cms_items.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
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.
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
<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:
1 2 3 |
<%= render :simpleCMS => "YourLabel", :admin => true %>
|
Here is another example with more options:
1 2 3 4 5 6 |
<%= 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.