Blog

Virtual Server virgin

Added by {{author}} {{age}} ago

walk through of first interaction, managing a Virtual Server with Parallels and Plesk

Guides : http://www.parallels.com/uk/products/plesk95/docs

http://download1.parallels.com/Plesk/PPP9/Doc/en-US/plesk-9.5-administrators-guide.pdf

MySQl and Rails 2.3 under Windows 7 - 193: %1 is not a valid Win32 application.

Added by {{author}} {{age}} ago

Error starting Rails : 193: %1 is not a valid Win32 application.

Tried all the usual triocks of copying thr dll to bin etc but doesn't resdolve issue, which seems specific to Windows 7

Answere here : Need to copy a very specific copy of libmysql.dll

http://stackoverflow.com/questions/1208029/193-1-is-not-a-valid-win32-application-bug-with-a-new-rails-application

http://osdir.com/ml/RubyonRailsTalk/2009-06/msg01775.html

JRuby OpenSSL issue

Added by {{author}} {{age}} ago

Soem wierd dependancy on openSSl that don't work when running some tasks e.g rake crm:setup

Error : OpenSSL::SSL requires the jruby-openssl gem

Install the gem.

jruby -S gem install jruby-openssl

Still get the error. So run with --trace

Issue on line 8 of /jruby-1.6.5/lib/ruby/site_ruby/shared/jruby/openssl/autoloads/ssl.rb

  1. try to activate jruby-openssl gem for OpenSSL::SSL, raising error if gem not present
    begin
    gem 'jruby-openssl'
    require 'openssl.rb'
    rescue Gem::LoadError => e
    raise LoadError.new("OpenSSL::SSL requires the jruby-openssl gem")
    end

Error is red herring, if you add a 'puts e' you see that error is :

jruby-openssl is not part of the bundle. Add it to Gemfile.

Migrate a Blog with interact

Added by {{author}} {{age}} ago

Interact is a gem to speed up interactions between ActiveRecord DB and other sources of data such as Excel or CSV files.

It provides some functions especially udeful for migrating data, so walkthrough will cover migrating a Blog from redmine to refinery

$ gem install interaction
1 gem installed

Within the Rail's app, in which you wish to us interact, edit top level Rakefile

gem 'interact   # Rails 2 only
require 'interact
Interact::load_tasks

For Rails 3 add gem to the bundle rather than Rakefile, perhaps in the 'development' group.

Export the data

We can export the existing Blog data from redmine, direct from mySql or phpmYAdmin as a CSV file, or we can use interact.

cd redmine

jruby -S rake interact:export:excel model=Blog result=blog.xls

Now we can generate an empty template for the target model

cd autocms
jruby -S rake interact:generate:excel model=BlogPost result=blog_template.xls

Now we can transpose the headers from the template over the existing data and save as a
new spreedsheet, that we can then use to load the data.

Copy the title column as is.

Copy the 'description' column over to the new 'body' column.

Can save as is if JRuby available or Save As csv to use normal Ruby.

Import data

rake interact:import:csv model=BlogPost input=blog_upload.csv

Downloadable for Spree

Added by {{author}} {{age}} ago

Add this line to your Gemfile:

gem 'spree_digital', :git => 'git://github.com/funkensturm/spree_digital.git', :branch => 'master'

bundle install

Copy the migration etc into Spree application and apply the migration.

rake spree_digital:install
rake db:migrate

Important!

Go to the spree admin section and create a shipping method that has the word download somehow in its name (it should be cost-free, but it doesn’t have to). It will be detected by spree_digital. Otherwise your customer will be forced to choose something like “UPS” even if they purchase only downloadable products.

Cucumber upgrades

Added by {{author}} {{age}} ago

Upgrading cucumber versions seems to change setup. Get errors such as

no such file to load -- cucumber/rails/active_record (LoadError)

Resolution : Reinstall cucumber helpers etc :

rails g cucumber:install

Using Refinery CMS

Added by {{author}} {{age}} ago

Walk though of creating new site using Refinary CMS

http://refinerycms.com/guides/getting-started-with-refinery

>gem install refinerycms
>refinerycms autotelik
>cd autotelik
>rails s

Open http:://localhost:3000

Follow instructions and create user

The refinerycms step creates the app, runs bundler, setups a sqllite DB all in one go.

Plugins

Add to Genfile

bundle install

Then for each one install + migrate for example :

    rails g refinerycms_blog
    rake db:migrate

First add Pages for all the main areas of the site, so the navigation links and content begins
to take shape, and can be reviewed in the default layouts.

Once basic structure in place can start to style and replace default views with our own design.

Refinery comes with a rake task called refinery:override which copies files out of Refinery gem and into the project's 'app' area, e.g app/views, app/models etc .

To see all possible commands simply run rake refinery:override in the console. To override the page's 'show' view:

rake refinery:override view=pages/show

Standard layout includes a refinary toolbar which we want to get rid off for the real site, so
a good place to start override is the main layout and remove render of /shared/site_bar

rake refinery:override view=layouts/application

Note: Need to specify over-ride basename only, e.g 'application' as task will not find or copy application.html.erb

As well as over riding the rails views we can now start to add our own styles.

No need to run rake override as an empty public/stylesheets/application.css is already provided so we just need to Open it up and crack on, for example to supplement the body styles with our own background image

body {
background-image: url(/images/autotelik/blue_circle.jpg);
background-repeat: no-repeat;
}

An Engine

http://refinerycms.com/guides/getting-started-with-refinery#extending-refinery-with-your-first-engine

Want to be able to link an image to a page, as a logo :

First generate the engine :

rails generate refinery_engine logo page:reference image:image alt:string

Now under vendor/engines we have new directory logos

Check and edit the new migration, then when happy follow instructions and run

bundle install
rails generate refinerycms_logos
rake db:migrate

------------------------

Edit logo model, the image referernce will be auto generated but need to add in

belongs_to :page

We also want to extend the existing Page model so we first create a decorator file under our engine's models directory

Then we extend the Page class, decorated with our new association

Page.class_eval do
  has_one :logo
  has_one :logo_image, :through => :logo, :as => :image
end

For this file to be picked up we need to edit the engine definition in lib/refinerycms-logos.rb

  def self.activate
        Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
          Rails.env.production? ? require(c) : load(c)
        end
  end

  config.to_prepare &method(:activate).to_proc

Server Issues

This does not necessarily work so well on server where access to gem installs may be more restricted.

Firstly Rails seems to insist somewhere in its depth on mysql2 - so need to update Gemfile

gem 'mysql2', :require => 'mysql2'

However bundle install --deployment seems to ignore this gem.

See good write up here : http://stackoverflow.com/questions/3642085/make-bundler-use-different-gems-for-different-platforms (known issue with Bundler)

Essentially Gemfile.lock created on Windows, but we're deploying on Linux, and Gemfile.lock contains platform specifics mysql (2.8.1-x86-mingw32)
mysql2 (0.2.6-x86-mingw32)

This will also affect devise once application running (creation of user will crash) as it will not find bcrypt-ruby (2.1.4-x86-mingw32)

The work around is to manually edit lock file, remove the "-x86-mingw32" portions from lines and rerun

bundle install --deployment

Next issue :
No such middleware to insert after: "ActionDispatch::Static"

The work around is to edit config/environments/production.rb and set

config.serve_static_assets = false

Issue : Where are all the migrations !? rake db:migrate does nothing.

Check ad sure enough nothing in db:migrate and no obvious rake task to copy over assets

Solution: Run

rails generate refinerycms --update

Issue : Error raised when visiting root url : ActionView::Template::Error (No expansion found for :defaults):

Solution: Remove <site_root>/app/views/layouts/application.html.erb

Publishing Open Source Project on GitHub

Added by {{author}} {{age}} ago

Active Record Loader is a file based database loader.

It maps file column names to model attributes and associations, enabling population of the database directly from Excel Spreedsheets (via Apache POI and JRuby) or flat files.

This is obviously slower than other forms of bulk upload, but spreedsheets can be an easy format for non technical users to populate, and by utilsing the ActiveRecord model, complex associations can be seamlessly maintained, and the embedded business logic, such as validations, is preserved.

This is going to be released under an open Source licence without restrictions. Not something I know lots about, but the MIT licence seems a reasonable fit.

First push the code to github :

Click 'Create A Repository' from main github Dashboard page.
Fill in details, access to the Repo for 'Anyone', and click Create.

Github will guide you through the repo population, stuff like

  git init
  git add .
  git commit -m 'first commit'
  git remote add origin git@github.com:autotelik/AR-Loader.git  

etc

Run rake task to build the gem (gemsepc defined in Rakefile to drive Rake::GemPackageTask.new(spec) )

rake gem

Sign up for account at: https://rubygems.org/users/new

Now publish the gem :

gem push pkg/ar_loader-0.0.1.gem

Enter email credentials and then ... happy days ...

Signed in.
Pushing gem to https://rubygems.org...
Successfully registered gem: ar_loader (0.0.1)

Add links for source code/wiki/documentation etc, via the Rubygems dashboard - find new gem and click 'Edit'

Source Code URL : https://github.com/autotelik/AR-Loader

Notes:
https://rubygems.org/pages/gem_docs
http://adam.heroku.com/past/2009/10/3/instant_gem_publishing_with_gemcutter/

Rails 3, Spree, BDD, Cucumber, RSpec

Added by {{author}} {{age}} ago

**h1. BDD with Spree

New Project so attempting to use BDD, for first time, from the very top.

Have a new Spree site ready, so first we need the BDD tools, RSpec and Cucumber, which we pull in when RAILS_ENV is dev or test, by adding following to Gemfile, then rerun bundle

group :development, :test do
  gem 'rspec-rails'
  gem "cucumber-rails" 
  gem "webrat" 
end

Now we can install the BDD tools dirs/files :

rails generate rspec:install
rails generate cucumber:install

Check the test setup :

rake db:test:prepare
rake spec
rake cucumber

NOTE: For coloured output Windows users should download this and wack it in your PATH => http://adoxa.110mb.com/ansicon/

First Feature

Site requires the concept of Membership so start with the admin function of creating a new Membership. Create a membership.feature file in features.

Feature: Admin creates Membership details
    As an Admin
    I want to navigates to admin url AND click 'new membership'
    So that I can enter a new Membership details

    @wip
    Scenario: enter new Membership
        Given I am logged in as Admin and I enter details of Membership
        When I click save Membership
        Then a Membership should be saved

NOTE : The @wip keyword can be used to select only certain scenarios to run - by running special rake task cucumber:wip (provided by the cucumber.rake fine generated in lib\tasks.)

Running rake cucumber:wip shows the step definitions required so we paste these into new file,
features/step_defintions/membership.steps and start to fill in the steps with the code we would like to be able to run, for example

Given /^I am logged in as Admin and I enter details of Membership$/ do
   if(current_user.admin?)
    @member = Membership.new( :name => 'Bronze', :duration => '1 Year' )
  end
end

Again we get failures such as "uninitialized constant Membership(NameError)". These point us to the code we need to add, and we are guided to create new Membership model (in extensions site/models), add admin Controller and create migration.

The next error to tackle is "undefined local variable or method `current_user' for #<Cucumber::Rails::World:0x30e8ec0> (NameError)"

Spree uses the Devise gem for authentication (Devise::SessionsController) so most of the code should already be in place, but looks like I was nievely expecting to get a handle on Controller methods (current_user) within Cucumber.

Good pointers here :

http://www.francisfish.com/2010/03/05/debugging-cucumber-scripts-cucumber-and-devise-authentication

First change the Feature to be more useful ny accepting the logged in user :

Given /^I am logged in as (.)$* and I enter details of Membership$/ do |email|

Might be some helper methods required to mock up the user.

def create_my_user(params)
  unless user = User.find_by_email(params[:email])
    params[:password_confirmation] = params[:password]
    user = User.create!(params)
    ## This makes the user look 'confirmed'
    user.update_attribute(:confirmation_token,nil)
    user.update_attribute(:confirmed_at,Time.now)
  end
  user
end

Given /^I am logged in as (.*)$/ do |email|
  @current_user = create_my_user(:email =&gt; email, :password =&gt; password )
  visit new_user_session_path
  fill_in("Email", :with => email )
  fill_in("Password", :with => password )
  click_button("Sign in")
  response.body.should =~ /My Lovely App/m
end

Browser

BDD is all well and good, but comforting sometimes to see what's happening in an actual broswer, so generate some seed data, and setup some routes first.

Route definition has changed quite a bit in Rails 3,so for example, old style :

map.root :controller => taxons, :action => 'show', :slug=>['home']
Becomes : root :to => "taxons#show", :slug=>['home']

NOTE: If migrating from a Rails 2 site, checkout the task rake rails:upgrade:routes

To add the route for the admin panel

Rails.application.routes.draw do
namespace :admin do
resources :memberships
end
end

Seed data

Add gem 'seed-fu' to Gemfile

Rails 3, Spree - New project walkthrough

Added by {{author}} {{age}} ago

Walk through as I build my first Rails 3 project, which will utilise the latest Spree gem.

Get latest version of rails, spree etc

rvm gem install rails

Create the Repo and the base Rails project

rails new EasyAppBook -d=mysql
cd EasyAppBook 
git init
git remote add unfuddle git@autotelik.unfuddle.com:autotelik/eab.git
git config remote.unfuddle.push refs/heads/master:refs/heads/master

Add the Spree gem to the Rails 3 Gemfile (found in top directory)

gem 'spree', '0.40.2'

Then run

bundle install

Edit database.yml and then run

rake db:create

To copy the Spree Engine's migrations and public assets into the Rails application run :

rails g spree:site
rake spree:install

Don't want any sample data so get straight into

rake db:bootstrap

Start server and navigate to http://localhost:3000 and check that site loads ok with no Products.

Add a .gitignore file to directory to ignore log files etc and add/commit the working site.

Now we can begin customisation.

Test the extension system by creating 2 extensions. I like the flexibility extensions give, to compartmentalize aspects of each site, so I will create a site_theme extension, to hold all layout, views, css etc and a 'site' extension to contain any new models, controllers or library code for the site.

  rails g spree:extension site
  rails g spree:extension site_theme

NOTES
Extension migrations need to be copied from the engine into the main Rails application.

You can do this with a convenient rake task.

rake site:install

This copying of assets, is also required for all 3rd party extensions as per this example :

Add `gem “spree_static_content”` and `gem “spree_editor” to Gemfile

    bundle install
    rails g spree_static_content:install
    rake db:migrate
    rake spree_editor:install

These install tasks also copy over public assets in your engines public directory.

If you just want the migrations you can run

rake site:install:migrations

Or if you just want the assets you can run

rake flag_promotions:install:assets.

The install.rake file is automatically created for you by the Spree extension generator. It will only be necessary until Rails 3.1 is released – at which point this task will be provided “automagically.”

NOTES :

When deploying the site on host use the --deployement flag for bundle to install gems locally and in isolation

bundle install --deployment

1 2 3 ... 5 Next »