Rails engine extends config/application.rb - ruby-on-rails

I am in the process of writing a Rails engine but i am not sure how to extend my the config/application.rb
I guess i have to get to the application name somehow
any ideas?
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module application_name
class Application < Rails::Application
end
end

For a --full and --mountable engine
This will be generated for you.
module engine_name
class Engine < ::Rails::Engine
end
end
In you main applications gemfile add
gem 'engine_name', path: "/path/to/engine_name"
And in your applications config/routes.rb file
mount engine_name::Engine, at: "/<mount_point_you_choose>"
http://guides.rubyonrails.org/engines.html
Taken from the link above...
The --mountable option tells the generator that you want to create a "mountable" and namespace-isolated engine. This generator will provide the same skeleton structure as would the --full option, and will add:
Asset manifest files (application.js and application.css)
A namespaced ApplicationController stub
A namespaced ApplicationHelper stub
A layout view template for the engine
Namespace isolation to config/routes.rb:

Related

How to autoload classes to use in controller

I have a class in libs and try to use it in a controller. However I cannot access it. I tried to use the autoload function, but that doesn't work, and it shouldn't work in rails 5 in production mode, so I guess I don't need to try this one.. I also tried to require it in my controller, but I don't get the syntax correct I guess. I'm also wondering where to put my class, since I have read several different opinions..
config/application.rb
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Qipmatedevel
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
config.active_job.queue_adapter = :sidekiq
config.autoload_paths += %W(#{config.root}/lib)
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
end
end
app/controller/imports_controller
class ImportsController < ApplicationController
require 'lib/class_qip'
adding
require './lib/class_qip.rb'
fixed it
Put your classes in lib directory only & load as,
config.eager_load_paths << Rails.root.join('lib')
It works on both development and production environment.

Rails 5 API - error autoloading helper when using the api_only flag

I'm trying to use a module from the folder 'app/helpers/transaction_helper.rb'. (It's important to note that the application is using the api_only flag, and helpers aren't being generated or loaded by default.)
module TransactionHelper
module Generator
def self.code_alphanumeric(params)
end
end
end
I got this error:
NameError: uninitialized constant TransactionHelper
I tried to add the following line to the application.rb
config.autoload_paths += %W(#{config.root}/app/helpers)
require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module SistemaControleContasApi
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/app/helpers)
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
end
end
But this until not work. If I try to put this file in the app/models directory, this work. However, this is not the right local to place a helper.
Could someone help, please?
Normally everything under app is autoloaded by default if you follow the Rails™ naming conventions. See here Docs.
set_autoload_paths: This initializer runs before bootstrap_hook. Adds
all sub-directories of app and paths specified by
config.autoload_paths, config.eager_load_paths and
config.autoload_once_paths to
ActiveSupport::Dependencies.autoload_paths.
So in the first step you should remove config.autoload_paths += %W(#{config.root}/app/helpers) from your config.
Then for every module you should create a subfolder.
So for your problem you should put your module under app/modules/transaction_helper/generator.rb
Also you don't need self in a module scope.
Personally I would create the helper as follows:
module TransactionHelper
def generate_alphanumeric_code
# params are automatically available in helpers.
end
end
And put it under app/modules/transaction_helper.rb.

Rails 5 project from API mode to non api mode

A while ago I started a Rails 5 project in API mode, but I would like now for the app to also start rendering html and serving assets.
This is what my application.rb looks like now:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module AppName
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
end
end
Notice there's no mention anymore of api_mode.
However, when I try to run the server, I'm getting the following error:
/home/username/Sites/oneroster/app/controllers/application_controller.rb:8:in `<class:ApplicationController>': undefined method `helper_method' for ApplicationController:Class (NoMethodError)
from /home/username/Sites/oneroster/app/controllers/application_controller.rb:1:in `<top (required)>'
What else should I do to make the app work again?
In application_controller.rb I had
class ApplicationController < ActionController::API
That had to be changed to:
class ApplicationController < ActionController::Base

Rails gem developement coping resources to the application

In my gem (myEngine.rb) I have the following code :
require "myEngine/version"
require "myEngine/models/contact"
require 'rails/generators'
module myEngine
def self.generate_migration
file_name= "migrationSrc.rb"
src_path= "myEngine/migration_files/"+file_name
destination_path= "db/migrate/"+file_name
//
end
end
When I call the method generate_migration I want my gem to copy the migration file to the db/migrate of my application.
Thanks you for your help.

How is this an un-initialized constant. (Mixins in rails)

under lib/ I have 'aisis_writer/loader.rb' which, inside that file, looks like:
module AisisWriter
module Loader
end
end
I then have, in my application.rb the following set up:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module AisisWriter
class Application < Rails::Application
# Load Lib
config.autoload_paths += %W(#{config.root}/lib)
# Use Rack Attack For Throttling
config.middleware.use Rack::Attack
end
end
From there I did, in the ApplicationController.rb: include AisisWriter::Loader and then I ran my tests and got:
'<class:ApplicationController>': uninitialized constant AisisWriter::Loader (NameError)
Either I cannot do what I am doing because of naming conflicts or I am doing something wrong. Any one care to tell me what I might be doing wrong?
I don't think your config.autoload_paths is broad enough -- it's not including subfiles of the lib directory.
This should do the trick:
config.autoload_paths += Dir[Rails.root.join('lib', '{**/}')]
Try defining like this within 'aisis_writer/loader.rb'
module AisisWriter::Loader
end

Resources