Warble Custom war file name - ruby-on-rails

I am using Wabler gem to generate the war file for our Ruby on Rails application. By default, it generates a WAR file named the Rails project home directory name by default.
I would like to know if it is possible to specify custom war file name instead of the default project home directory name.
Thanks
Noman A.

Found answer to my question, so sharing with all who might need it.
File to modify:
warble.rb
For our application, this resides inside [project_dir]/config/
This file would have a section like:
Warbler::Config.new do |config|
.....
.....
end
Add the following line to specify custom war/jar file name (without extension):
config.jar_name = "testname"
So, the segment would look like:
Warbler::Config.new do |config|
.....
.....
config.jar_name = "testname"
end
Note: The "...." shown above are not part of the actual configuration, I have replaced original values for example purpose.
Now, when I run warble war/ rake war, it generates war file as "testname.war"
Hope this might help another newbie like me!
Thanks
Noman A.

Add config/warble.rb file.
Warbler::Config.new do |config|
config.war_name = "test_war_name"
end
Edit: config.war_name is deprecated, see logged here
# Deprecated
def war_name
$stderr.puts "config.war_name deprecated; replace with config.jar_name" #:nocov:
jar_name #:nocov:
end
Use the following:
Warbler::Config.new do |config|
config.jar_name = "test_war_name"
end

Related

I moved my folder from lib to /app in a rails 6, I can't seem to instantiate an object still because of load path issue

I have a new rails 6 application and in the lib folder I had this:
/lib/some_app_name/stripe/subscription/subscription_service.rb
module Someappname # Someappname is also in my application.rb
module Stripe
class SubscriptionService
def initialize(a)
#a = a
end
end
end
end
I then moved the 'some_app_name' folder to:
/app/some_app_name/stripe/subscription_service.rb
I read that anything inside of /app will be autoloaded and reloaded so I moved in here. It wasn't working in /lib also.
In my home_controller.rb I tried this:
ss = Someappname::Stripe::SubscriptionService.new("a")
I get an error saying:
uninitialized constant Someappname::Stripe::SubscriptionService
What am I doing wrong here?
I suspect it's spring, try this
bin/spring stop
And then start rails console, stopping Spring will force Rails to load your app fresh
Also,
if the name of your module is Someappname, then the directory name should be app/someappname and not some_app_name
Hope that helps!
Rails does auto-load everything under /app, but there's a caveat. First level of directories is not becoming a part of module name. That's why you can have class User in /app/models/user.rb (and not class Models::User).
Solution: place your code into some folder under /app. I usually call it /app/lib or /app/custom or something like that.
/app/custom/some_app_name/stripe/subscription/subscription_service.rb
(and yes, make sure that names in your filepath correctly represent names in your module path. You can't have directory some_app_name for module Someappname, but you can for SomeAppName)

Ruby on Rails Configuration - Plaid Gem

I am trying to integrate the plaid gem into one of my projects. From the docs it says to configure it as such.
https://github.com/plaid/plaid-ruby
Plaid.config do |p|
p.customer_id = 'Plaid provided customer ID here'
p.secret = 'Plaid provided secret key here'
p.environment_location = 'URL for the development or production environment'
end
My question is what file should I have this code? Should it go in my application.rb? config.rb? Just want to make sure I'm following convention. Thanks!
You should create this file in config/initializers directory with any file name i.e. plaid.rb.
FYI: Using Initializer Files.

Rails mountable engine: how should apps set configuration variables?

I have a mountable engine called Blog that apps can use.
What's the best way to allow apps that use the engine to set a configuration variable like site_name (so that the engine can display it in the design)?
Update:
I've seen some gems create a 'config/initializers/gem_name.rb' file. Is there any specification on how to:
Create a file like that on the engine's side
Copy it into the app's side
How to access those set variables on the engine's side?
I tried creating Blog.site_name = "My Site" in the app's config/initializers/blog.rb file but get an Undefined method error.
Figured out an even better solution that also allows you to set default values (incase the app using the engine doesn't specify a config)...
Create config variables in your app's /config/initializers/blog.rb like this:
Blog.setup do |config|
config.site_name = "My Site Name"
end
In your engine's /lib/blog/engine.rb set default values like this:
module Blog
self.mattr_accessor :site_name
self.site_name = "Site Name"
# add default values of more config vars here
# this function maps the vars from your app into your engine
def self.setup(&block)
yield self
end
end
Now you can simply access the config variables in your engine like this:
Blog.site_name
Much cleaner.
After a lot of testing and looking into existing gems, here is what works in Rails 4:
Considering your engine's name is Blog:
In your engine's /lib/blog/engine.rb put in this:
module Blog
def self.setup(&block)
#config ||= Blog::Engine::Configuration.new
yield #config if block
#config
end
def self.config
Rails.application.config
end
end
In your app, create a file called /config/initalizers/blog.rb and set config vars like this:
Blog.setup do |config|
config.testingvar = "asdfasdf"
end
Then you can access these config variables ANYWHERE in your engine like this:
Blog.config.testingvar
Hope this helps someone. There is very little documentation on this right now so it was all trial and error.
I know this is a fairly old post, but in the event someone in the future finds this, I'd like to recommend the Angostura gem for passing dependencies into a Rails engine. To use it, assuming my engine is called 'Blog' and I want to access a variable called 'site_name', the engine's lib/blog.rb looks something like:
require "blog/engine"
require "angostura"
module Blog
include Angostura::Dependencies
dependency :site_name
end
In my main app, in config/initializers/blog.rb, I added
Blog.setup do |config|
config.site_name = "My site name"
end
Now, I can access site_name in my engine by calling Blog.site_name.
I'd like to point out that defaults are also supported, so you could do something like dependency site_name: 'Default site name' in lib/blog.rb. Furthermore, you can pass in whole classes as dependencies by sending it stringified classnames, like config.my_class = 'MyClass'.
For posterity, in order to use the gem, I added s.add_dependency "angostura", "0.6.0" in my engine's gemspec, and then ran bundle install.

How to access a yaml in rails config path

I'm writting a ruby gem and I want to let user use your own yaml file configuration, but I don't know how to test if the user_config.yml exists into rails config path.
I want to use user_config.yml only if this file exists into config path.
Rails.root (or RAILS_ROOT in earlier versions of Rails) gives you the path of the application root. From there, you can see if the file you're interested in exists.
e.g.
path = File.join(Rails.root, "config", "user_config.yml")
if File.exists?(path)
# do something
end
you can access your Rails App's load path with the $LOAD_PATH variable which is of type Array.
Thus
if $LOAD_PATH.include?('/path/where/user/should/put/their/user_config.yml')
# EDIT
config_options = File.new('rails_app/config/user_config.yml', 'r')
else
# EDIT
config_options = File.new('path/to/default/config.yml', 'r')
end
There is also another useful method for the Ruby File class called #exists? which will of course check to see if a file exists. See File class docs for more.
Hope this gets you started. Your question was rather vague, reply with more details if you need more help.

sandbox testing environments exist for TDD'ing command line application

I'm writing a command line command but want to TDD it. I'll be creating and deleting files and was wondering if there's a sandbox testing gem or something like that. I'm using ruby and rspec.
Depends on what you're trying to do, but I test most of my command line Ruby by mocking out the file system and STDIN/STDOUT. Using dependency injection I often end up with something along these lines:
describe Add do
it 'writes the result to standard out' do
console = mock('STDOUT')
console.should_receive(:puts).with('5')
Add.new(console).execute(3,2)
end
end
class Add
def initialize(out = STDOUT)
#out = out
end
def execute(command_line_args)
#out.puts(command_line_args.inject(:+))
end
end
Add.new.execute(ARGS)
By using default values I can inject in the test, but leave it out of the production code.
Hope that helps!
Brandon
The template generated by the newgem install_cucumber generator uses a pattern that I like quite a bit. Have a look at the support/env.rb and support/common.rb files it creates:
https://github.com/drnic/newgem/blob/master/rubygems_generators/install_cucumber/templates/features/support/env.rb.erb
https://github.com/drnic/newgem/blob/master/rubygems_generators/install_cucumber/templates/features/support/common.rb
Use of it in test looks like this:
in_tmp_folder do
# The current directory is now a generated tmp folder.
# If you stick to relative paths, everything you do in here should be safe
end
The files linked to above are for using this in cucumber tests, but it could easily be adapter to whatever framework you're using. The env.rb above deletes the tmp folder before each test starts.
You might also want to take a look at the sandbox gem.
gem install sandbox
Example usage is here: https://github.com/bdimcheff/sandbox

Resources