Показаны сообщения с ярлыком Ruby on Rails. Показать все сообщения
Показаны сообщения с ярлыком Ruby on Rails. Показать все сообщения

How to implement HVMC in Rails




The problem is how to implement this pattern in Rails.

Rails engines? No!
It is a great way to make "pluggable" applications. But if you want to make request to some engine this request should block another instance of your server. So, if you have high-level HMVC structure you must have a lot of  rails-instances to serve your application.

Solution: async-rails
I think, that nice solution of this problem is a composite of Rails engines and async-rails. So let's rock!

Let's assume that we have an engine that implements some FooController with a #bar action:
module MyEngine
  class FooController < ::ApplicationController
    def bar
      render the: text => "Your text is #{params[:text]}"
    end
  end

end

Add to your Gemfile:

gem 'thin' # yes, we're going to use thin!
gem 'rack-fiber_pool', :require => 'rack/fiber_pool'


# async http requires
gem 'em-synchrony', :git => 'git://github.com/igrigorik/em-synchrony.git',  :require => 'em-synchrony/em-http'
gem 'em-http-request',:git => 'git://github.com/igrigorik/em-http-request.git', :require => 'em-http'
gem 'addressable', :require => 'addressable/uri'




Add this line to config.ru: 
...
use Rack::FiberPool # <-- THIS ONE
run HmvcTest::Application

Add this to your config/application.rb:

...
    config.threadsafe! # <-- THIS ONE
  end
end


Now we can make a request from any part of our application to the FooController:


class HomeController < ApplicationController
  def index
    http = EM::HttpRequest.new("http://localhost:3000/foo/bar?text=#{params[:a]}").get


    render :text => http.response
  end
end


Now you can start thin:
$ thin -D start

and open http://localhost:3000/home/index?a=HMVC in your browser:

In Rails log we have:
Started GET "/home/index?a=HMVC" for 127.0.0.1 at 2012-02-03 15:27:02 +0400
Processing by HomeController#index as HTML
  Parameters: {"a"=>"HMVC"}


Started GET "/foo/bar?text=HMVC" for 127.0.0.1 at 2012-02-03 15:27:02 +0400
Processing by FooController#index as HTML
  Parameters: {"text"=>"HMVC"}
  Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.6ms | ActiveRecord: 0.0ms)
  Rendered text template (0.0ms)
Completed 200 OK in 6ms (Views: 0.4ms | ActiveRecord: 0.0ms)

We have one request inside the other. So we done!

Problems
This approach has some disadvantages:
  • there is no way to isolate engine from outside world (everybody can make request directly to FooController)
  • all logs are written to the same file
Links



Еще одно решение для расшаривания локального сайта

По какой-то причине сегодня перестал работать proxylocal. Хорошо, что быстро нашелся аналогичный сервис: localtunnel.

Использование предельно простое:

$ localtunnel 8080
    Port 8080 is now publicly accessible from http://8bv2.localtunnel.com ...


Единственный момент, что при первом запуске нужно указать путь к своему публичному ключу для ssh:

$ localtunnel -k ~/.ssh/id_rsa.pub 8080


сайт: http://progrium.com/localtunnel/
github: https://github.com/progrium/localtunnel



Ruby on Rails - Multiple Database connections

I've found elegant way to configure multiple database connections on Rail at PullMonkey Blog. Check this out.



Simple nicEdit plugin for Ruby on Rails

Yesterday I've posted new Ruby on Rails plugin Simple nicEdit. It helps to use tiny rich edtior nicEdit in your rais application.

It's supposed, that you are already using jrails plugin, that replaces prototype with jQuery. If not, than install it:
$ ruby script/plugin install git://github.com/aaronchi/jrails.git

Installing Simple nicEdit plugin:
$ ruby script/plugin install git@github.com:sergio-fry/Simple-nicEdit.git

Add js to your layout:
<%= javascript_include_tag 'nicEdit', 'nicEditInit' %>

Use it in form
<% form_for @page do |f| %>
  <%= f.rich_text_area :body %>
<% end %>

Result:










Good Luck!




2008–2014 © Сергей Удалов Реклама Система Orphus