How to run a Rails application in stone?

I'm not sure that such a thing is very common, but I keep trying to create gems that are just wrappers around the Rails application.

My gem will have a generator to create config.ru, but the Rails application will live inside the gem lib directory. I need to know how to "embed" a Rails application and configure it so that it can run inside the gem.

For instance:

$ mygem new project

mygem created a directory called "project" with the following files:

project/config.ru
project/widgets/
project/foobars/

My gem will also create some directories that need to be added to Rails one way or another so that I can access the code in these directories from a Rails application living inside the Gem.

Any help or advice you can give me will be appreciated.

, Rails Rails. Rails-, , ( rails), , Rails .

: , . gem lib.

$ bundle gem my_gem && cd my_gem/lib
$ rails new my_gem --skip-bundle

:

my_gem/
  my_gem.gemspec
  bin/my_gem
  lib/
    my_gem.rb
    my_gem/
      version.rb # generated by bundler
      # the rails app:
      app/
      config/
      Gemfile
      ...etc

Rails, , Rails Gemfile, gem Gemspec, , assets Gemfile.

# Rails Gemfile
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'therubyracer', :platforms => :ruby
  gem 'uglifier', '>= 1.0.3'
end

# gemspec
Gem::Specification.new do |gem|
  gem.name          = "my_gem"
  # ...
  gem.add_dependency 'rails', '3.2.8'
  gem.add_dependency 'sqlite3'
  gem.add_dependency 'jquery-rails'
  # how to add the assets group gems?
end
+5
3

, .

- , .

:

Rails Rails:

$ bundle gem my_gem

Rails:

$ rails new my_app --skip-bundle

Rails :

$ cp -R my_app/* my_gem

Rails:

$ cd my_gem
$ bundle install --binstubs --path vendor/bundle
$ cd -

Rakefile Rails:

#!/usr/bin/env rake
require "bundler/gem_tasks"
require File.expand_path('../config/application', __FILE__)
MyApp::Application.load_tasks

, :

$ rails server

:

, Rails , "" , config/application.rb , :

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{config.root}/../customdir )

"..", , Rails. .

, , , , "~/myfiles/". ENV vars , .

, , , :

$:.unshift File.dirname(__FILE__)

Gem Build:

my_gem.gemspec, , , .., :

$ gem build my_gem.gemspec
Successfully built RubyGem
Name: my_gem
Version: 0.0.1
File: my_gem-0.0.1.gem

Rails .

config.ru Rails. AFAIK.

:

$ gem install my_gem

gem. , . rubygems: http://docs.rubygems.org/read/chapter/3

Crate:

Crate:

Rack:

config.ru Rails:

# Rails.root/config.ru
require "config/environment"

use Rails::Rack::LogTailer
use ActionDispatch::Static
run ActionController::Dispatcher.new

Rails. Ruby "require" , LOAD_PATH.

:

# Rails.root/config.ru
require_relative 'filename'
require "config/environment"

:

require './../../filename'  # not the best for security

, File.expand_path:

File.expand_path(__FILE__)

:

$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'filename'

. , !

+11

: " Rails ?".

Rails -. , , . - . .

, , , , ActiveRecord, Rails.

-, Rails, . gemspec .

, Rails, .

0

, , , , . , , - -, . - Bowline. , , , , , , github zip . . , - - Fat Free CRM github.

0

All Articles