Skip to content

finiteautomata/rails-engine-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  1. Los Engines pueden ser considerados como "subaplicaciones" dentro de una aplicación más grande. De hecho, una aplicación Rails comparte muchísima funcionalidad con un Engine, con lo cual "son casi lo mismo"

Creamos un engine

$ rails plugin --help Ver las opciones

$ rails plugin new mathmath --full --mountable --skip-test-unit --dummy-path=spec/dummy

  1. En engine.rb podemos observar

isolate_namespace Mathmath

"The isolate_namespace method here deserves special notice. This call is responsible for isolating the controllers, models, routes and other things into their own namespace, away from similar components inside the application"

  1. El Gemfile y gemspec son idénticos a los de una gema, como explicó Luis en su momento

  2. En app/ encontramos las mismas cosas que en una aplicación común y silvestre.

  3. En spec/dummy vamos a encontrar una aplicación que utiliza nuestro engine, y nos sirve para probarlo.

Si vemos dentro de su routes.rb, veremos la línea que lo monta..

A su vez, dentro de boot.rb, tenemos esta línea

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)

require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
  1. Creemos un controller

$ bin/rails g controller tables

con...

require_dependency "mathmath/application_controller"

module Mathmath
  class TablesController < ApplicationController
    respond_to :json

    def show
      multiplier = Integer(params[:id])

      tables = Hash[(1..10).collect {|x| ["#{x} * #{multiplier}", x * multiplier]}]

      respond_with tables
    rescue ArgumentError
      respond_with({error: "#{params[:id]} is not a valid multiplier"})
    end
  end
end

y agregamos el route.

Lo mostramos

  1. Crear modelos

br g model SpecialNumber ...

  • Metemos seeds de paso
Mathmath::SpecialNumber.create name:"e", value: 2.71828, description: <<-EOS
  The number e is an important mathematical constant that is the base of the natural logarithm. It is approximately equal to 2.71828,[1] and is the limit of (1 + 1/n)n as n approaches infinity, an expression that arises in the study of compound interest. It can also be calculated as the sum of the infinite series[2]
  EOS
  1. Creamos una nueva app que use nuestro engine

$ rails new newapp

  • Agrego en el Gemfile gem 'mathmath', path: "../mathmath" o gem 'mathmath', github: "geekazoid/mathmath"

  • Agrego en el routes.rb

mount Mathmath::Engine, at: "mathmath"

  • Instalo migraciones $ bin/rake mathmath:install:migrations

  • Agrego ésto (para cargar seeds) Mathmath::Engine.load_seed

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published