I've got an app with multiple deployments in different languages. In order to streamline testing and deployment I'd like to write a quick rake task that sets the locale.
I've tried different ways but I'm stuck actually accessing i18n.default_locale from the Rake task.
I'd like to do something like
bundle exec rake locale:set the_locale_to_switch_to
So far I've got (full of syntax errors)
task :set, [:locale_name] => :environment do |t, args|
new_locale = args.locale_name
puts "Changing locale to #{new_locale}"
ApplicationController.config.I18n.default_locale = new_locale
puts "locale is #{ApplicationController.config.I18n.default_locale}"
end
Any way to do that? I'd like to modify an already-deployed app if possible... am I going the right way?
Related
I am new in ROR and im using version Ruby 1.9.3 & Rails 3.1.0.
I want to generate rake task. How to generate rake take?
i am using below code for generate rake task in commend prompt.
rails generate task permission my_task1 my_task2
But every time give message "Could not find generator task"
Please help.
You create a new file in lib/tasks like lib/tasks/your_task.rake.
You add a new task to that file:
namespace :my_app do
desc "a new task to be executed"
task :my_task do
puts "hello rake"
end
end
This would be called like rake my_app:my_task
Update
The task generator was added in Rails 3.2.0
Haven't found the reason for this question why rails generating error. So i have done bit searching somewhere else and found this. May this help.
Use this instead -
bundle exec rails g task permission my_task1 my_task2
Rails is not able to find task so this is a work around to get you going.
src - [https://railsguides.net/how-to-generate-rake-task/]
syntax: rails g task permission my_task1 my_task2
namespace :permission do
desc "relevant to your task functionality"
task :my_task1 => :environment do
end
desc "relevant to your task functionality"
task :my_task2 => :environment do
end
end
I want to prepopulate my cache with an initializer, but I don't need this code to run every time I run rake or rails g, etc. Rake and Bundler are easy to deal with, but a similar solution does not work for the generators:
# config/initializers/prepop_cache.rb
if !defined?(::Bundler) and !defined?(::Rake) and !defined(Rails::Generators)
# do stuff
end
This must be because rails/generators (or something similar) is requireed at runtime. How can I check to see if the command being run is rails g xyz?
Update:
Two solutions can be found here: Rails 3 initializers that run only on `rails server` and not `rails generate`, etc
Still would like to know if it's possible in the manner I've tried above.
In Rails 3, what you're looking to do is conceivably possible, but in a hacky way. Here's how:
When you make a rails generate call, the callpath looks like this:
bin/rails is called, which eventually routes you to execute script/rails
script/rails is executed which requires rails/commands
rails/commands is loaded, which is the main point of focus.
Within rails/commands the code that runs for generate:
ARGV << '--help' if ARGV.empty?
aliases = {
"g" => "generate",
"c" => "console",
"s" => "server",
"db" => "dbconsole"
}
command = ARGV.shift # <= #1
command = aliases[command] || command
case command
when 'generate', 'destroy', 'plugin', 'benchmarker', 'profiler'
require APP_PATH
Rails.application.require_environment! # <= #2
require "rails/commands/#{command}" # <= #3
The points of interest are numbered above. Namely, that at point #1 the command that you're running is shifting off of ARGV. Which in your case means generate is going to be removed from the command line args.
At point #2 your environment gets loaded, at which point your initializers are going to be executed. And herein is the tough part - because nothing indicating a specific command has been loaded at this point (this occurs at #3) there is no information to determine a generator is being run!
Let's insert a script into config/initializer/debug.rb to see what is available if we ran rails generate model meep:
puts $0 #=> "script/rails"
puts ARGV #=> ["model", "meep"]
As you can see, there is no direct information that a generator is being run. That said, there is indirect information. Namely ARGV[0] #=> "model". Conceivably you could create a list of possible generators and check to see if that generator has been called on ARGV[0]. The responsible developer in me says this is a hack and may break in ways you'd not expect so I'd use this cautiously.
The only other option is to modify script/rails like you suggested -- which isn't too bad a solution, but would likely break when you upgrade to Rails 4.
In Rails 4, you've got more hope! By the time the application environment is being loaded, the generators namespace has already been loaded. This means that in an initializer you could do the following:
if defined? Rails::Generators #=> "constant"
# code to run if generators loaded
else
# code to run if generators not loaded
end
Using deploy.rb to precompile rails assets only when they change, this task is always skipping the compile of my assets :(
namespace :assets do
task :precompile, :roles => :web, :except => {:no_release => true} do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
end
What could causing this complete task not compiling? It always thinks there are no asset changes and throws that message.
I also never really understood the task, for example what does below source.log.local refer to?
source.local.log
Could anyone clarify what the task commands do and has some pointers why it never sees any asset changes? Thank you
What it does:
from = source.next_revision(current_revision)
source is a reference to your source code, as seen through your SCM (git, svn, whatever). This sets from as (essentially) the currently deployed version of your source code.
capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l")
capture means 'execute this command in the shell, and return its output'. The command in question references the log of changes to your source comparing the deployed version to the current version (specifying the paths where assets live as the ones that 'matter'), and passes that into the word count tool (wc -l). The -l option means that it returns a count of the number of lines in the output. Thus, the output (which is returned by capture) is the number of filenames that have changes between these two versions.
If that number is zero, then no files have changed in any of the specified paths, so we skip precompiling.
Why it doesn't work:
I don't know. There doesn't seem to be anything wrong with the code itself - it's the same snippet I use, more or less. Here's a couple of things that you can check:
Does Capistrano even know you're using the asset pipeline? Check your Capfile. If you don't have load 'deploy/assets' in there, deploying won't even consider compiling your assets.
Have you, in fact, enabled the asset pipeline? Check application.rb for config.assets.enabled = true
Do you have incorrect asset paths specified? The code is checking for changes in vendor/assets/ and app/assets/. If your assets live somewhere else (lib/assets, for instance), they won't be noticed. (If this is the case, you can just change that line to include the correct paths.
Have you, in fact, changed any assets since the last deployment? I recommend bypassing the check for changed assets and forcing precompile to run once, then turning the check back on and seeing it the problem magically resolves itself. In my example, below, setting force_precompile = true would do that.
What I use:
Here's the version of this I currently use. It may be helpful. Or not. Changes from the original:
A more readable way to specify asset paths (if you use it, remember to set asset_locations to the places your assets live)
An easy way to force precompile to run (set force_precompile=true to attempt to run the check, but run precompile regardless)
It prints out the count whether or not precompile runs. I appreciate getting some output just to be sure the check is running.
If an error occurs when trying to compare the files (as will often happen with brand new projects, for instance), it prints the error but runs precompile anyway.
.
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
# Check if assets have changed. If not, don't run the precompile task - it takes a long time.
force_compile = false
changed_asset_count = 0
begin
from = source.next_revision(current_revision)
asset_locations = 'app/assets/ lib/assets vendor/assets'
changed_asset_count = capture("cd #{latest_release} && #{source.local.log(from)} #{asset_locations} | wc -l").to_i
rescue Exception => e
logger.info "Error: #{e}, forcing precompile"
force_compile = true
end
if changed_asset_count > 0 || force_compile
logger.info "#{changed_asset_count} assets have changed. Pre-compiling"
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
else
logger.info "#{changed_asset_count} assets have changed. Skipping asset pre-compilation"
end
end
end
I feel like this should be a simple problem, but I'm pulling my hair out trying to track it down. I'm installed the chargify_api_ares gem, but can't do even basic things such as
Chargify::Subscription.create
As I get this path error. I feel like this must be a gem issue somehow but don't know where to go from here.
UPDATE: bundle show chargify_api_ares shows the correct path, I just somehow can't access it. Still trying random environment related things.
Looks like this is the source of the problem, in active_resource\base.rb:
# Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>)
# This method is regenerated at runtime based on what the \prefix is set to.
def prefix(options={})
default = site.path
default << '/' unless default[-1..-1] == '/'
# generate the actual method based on the current site path
self.prefix = default
prefix(options)
end
As I understand it, Chargify.subdomain should be setting the site.path, but I don't understand activeresource well enough yet to know what's happening and will continue to dig.
I too had the same issue.
I executed the following on the console
Chargify.configure do |c|
c.api_key = "<<api_key>>"
c.subdomain = "<<subdomain>>"
end
After that performing any Chargify console commands went through fine.
I have a Rails 3 app which needs to have some directories created. I'd like to have a rake task which I can run to do this as a sort of initialization procedure. Basically I'd like to do: rake app:create_dirs or something similar. I tried using the "directory" commands but they seem to be only for dependencies in rake. Any ideas how to do this nicely? My dir structure needs to look like this:
public/content/0/0
public/content/0/1
public/content/0/2
...
public/content/1/0
public/content/1/1
...
public/content/n/m
where n is 0..9 and m is 0..9
Thanks for any advice.
Something like this should work, I don't know your exact application but the main point is to look into FileUtils#mkdir_p
require 'fileutils'
(0..9).each do |n|
(0..9).each do |m|
FileUtils.mkdir_p("#{Rails.public_path}/content/#{n}/#{m}")
end
end