How to Rake task generate in ROR - ruby-on-rails-3

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

Related

Laravel update database record running script

I want to update any records in my database running SQL script using artisan. For example, I need to execute such command:
UPDATE translations SET field = 'meta_desc' WHERE field = 'page_desc'
What the Laravel's sctructure will be the best solution? Seed, migrations, factories?
Thanks to everybody for replies. Ecpesially thanks to #RossWilson for his idea of using migrations for changing DB's data.
But I think, it's not a good solution, because the Laravel's concept involves using migrations for DB structure's changing.
After reading Laravel's manuals I've found that there is a special code structure for working with db's data. It's a Seed. So, I solved my issue using the next seed for my example query above:
Created the new seed using artisan command:
php artisan make:seed UpdateTranslationsSeeder
Write a code inside the run() method:
DB::table('translations')->where('field', 'page_desc')->update(['field' => 'meta_desc']);
Run my seeder in artisan:
php artisan db:seed --class=UpdateTranslationsSeeder
Note: if after running the last command in console I've got an error about class UpdateTranslationsSeeder is undefined run the next command in console to tell Laravel about new classes:
composer dump-autoload -o
I have adapted Paul Basenko approach to updating existing data for educational purposes. It was very useful! Here is my example:
Generate a seeder:
php artisan make:seed UpdateCustomerSeeder
Add code inside run method:
$customers = Customer::all();
$customers->each(function ($customer_update, $key) {
$faker = Factory::create('it_IT');
$customer_update->name = $faker->company;
$customer_update->website = $faker->domainName;
$customer_update->tax_number = $faker->vatId();
$customer_update->phone = $faker->phoneNumber;
$customer_update->email = $faker->safeEmail;
$customer_update->street = $faker->streetAddress;
$customer_update->city = $faker->city;
$customer_update->zip_code = $faker->postcode;
$customer_update->notes = $faker->sentence();
$customer_update->save();
});
Run seeder:
php artisan db:seed --class=UpdateCustomerSeeder

Why does rails scaffold generator is simply ignoring my config/iniatilizers/inflections.rb?

I'm using Rails 3.2 with ruby 2.1
I put this code into my config/initializers/inflections.rb :
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'pub_type_contributeur', 'pub_types_contributeurs'
inflect.irregular 'PubTypeContributeur', 'PubTypesContributeurs'
end
When I test it with my rails console it works :
rails console
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loading development environment (Rails 3.2.16)
2.1.0 :001 > "attendance".pluralize
=> "attendances"
2.1.0 :002 > "pub_type_contributeur".pluralize
=> "pub_types_contributeurs"
2.1.0 :003 > exit
But when I use the standard generator, it just ignore my inflection :
rails generate scaffold pub_type_contribueur nom:text -p
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
invoke active_record
create db/migrate/20141112191424_create_pub_type_contribueurs.rb
create app/models/pub_type_contribueur.rb
invoke rspec
create spec/models/pub_type_contribueur_spec.rb
invoke factory_girl
create spec/factories/pub_type_contribueurs.rb
invoke resource_route
route resources :pub_type_contribueurs
invoke scaffold_controller
create app/controllers/pub_type_contribueurs_controller.rb
invoke haml
create app/views/pub_type_contribueurs
create app/views/pub_type_contribueurs/index.html.haml
create app/views/pub_type_contribueurs/edit.html.haml
create app/views/pub_type_contribueurs/show.html.haml
create app/views/pub_type_contribueurs/new.html.haml
create app/views/pub_type_contribueurs/_form.html.haml
invoke rspec
create spec/controllers/pub_type_contribueurs_controller_spec.rb
create spec/views/pub_type_contribueurs/edit.html.haml_spec.rb
create spec/views/pub_type_contribueurs/index.html.haml_spec.rb
create spec/views/pub_type_contribueurs/new.html.haml_spec.rb
create spec/views/pub_type_contribueurs/show.html.haml_spec.rb
create spec/routing/pub_type_contribueurs_routing_spec.rb
invoke helper
create app/helpers/pub_type_contribueurs_helper.rb
invoke rspec
create spec/helpers/pub_type_contribueurs_helper_spec.rb
invoke assets
invoke coffee
invoke scss
invoke scss
What am I doing wrong ?
Ok some time we work too late and misspell name... sorry this is my bad, I forgot the "t" letter.
my command was incorrect :
rails generate scaffold pub_type_contribueur nom:text --no-stylesheets -p
This is the correct one :
rails generate scaffold pub_type_contributeur nom:text --no-stylesheets -p

Can I change my default locale via a rake task?

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?

Rails 3 and AWS::S3 Ruby Gem

I am following the documentation (http://amazon.rubyforge.org/) to try to begin working on S3 into my application (for serving files to the end user) but I keep running into errors.
Here is my Model:
class File < AWS::S3::S3Object
set_current_bucket_to 'test-bucket'
end
This is what I am trying via the Rails Console:
File.find 'test.pdf'
But I keep getting this error:
undefined method `find' for File:Class
Not sure what I am doing wrong here... anyone else run into this issue?
File is not a very good name for your model as it gets overwrited by Ruby's standart class File (docs). Just choose another name and everything will just work!

How to create a directory structure with Rake for a Rails project

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