Build extension spree e-commerce - ruby

I'm with one problem when executed the step 5.7. In my extension not metting the path:
#lib/spree/flag_promotion_configuration.rb.
I must create the directory 'spree' and after create the file 'flag_promotion_configuration.rb' ?
If I write this in the file, as the tutorial required:
#lib/spree_flag_promotions/engine.rb
module Spree::ActiveShipping; end
....
module SpreeFlagPromotions
class Engine < Rails::Engine
initializer "spree.flag_promotions.preferences", :after => "spree.environment" do |app|
Spree::FlagPromotions::Config = Spree::FlagPromotionConfiguration.new
end
....
end
end
And I executed:
$ rake db:migrate
Return this error:
rake aborted!
uninitialized constant Spree::FlagPromotions
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
I believe it has anything to do with:
#lib/spree/flag_promotion_configuration.rb.
Because they do not know where to create this file is or where.

Related

Rake in Ruby: undefined method `namespace' for main:Object (NoMethodError)

I'm currently trying to run a Rake task in a Ruby project (No Rails). What I am trying to accomplish is to run a method from a file within my Ruby project. However I get the following error:
undefined method `namespace' for main:Object (NoMethodError)
I created a folder task that holds a test.rb file. Before I had it as test.rake but I think this was incorrect. I also created a Rakefile pointing to task/test.rb
For redeability, I'm using namespace: although I'll be honest I'm not sure if I even need it.
#Rakefile
task :default => [:test]
task :test do
ruby 'task/test.rb'
end
Here is the task.test.rb
require './src/lambda_function.rb'
class KMS
def initialize
end
def decrypt(key)
return "some password"
end
end
class SNS
def initialize
end
end
TEST_FORM_ID=123
namespace :test do
namespace :lambda do
desc 'Run the Lambda process function'
task :process do
LambdaFunctions::LambdaHandler.process(box_api: BoxApi.new,
form: TEST_FORM_ID,
sns: SNS.new,
kms: KMS.new)
end
end
end
What I'm I doing wrong?

How do I programmatically extract a rake task's description?

I'm attempting to capture the equivalent of rake -D programmatically. I can load the Rakefile I'm targeting and see a list of tasks, but I can not figure out how to get the descriptions.
This will let me see the tasks that I am interested in:
Dir.chdir #myTarget
rake = Rake::Application.new
Rake.application = rake
rake.init
rake.load_rakefile
tasks = Rake.application.tasks
puts tasks.inspect
The above outputs something similar to:
[<Rake::Task default => [test]>, <Rake::Task foodcritic => []>, <Rake::Task integration => [kitchen:all]>]
My question is how to access the desc comments that are visible if I run rake -D. Here's what I'm after:
rake foodcritic
Run Foodcritic lint checks
rake integration
Alias for kitchen:all
rake kitchen:all
Run all test instances
Here's the final solution. The key was that I was missing metadata from the taskmanager:
Dir.chdir #myTarget
rake = Rake::Application.new
Rake::TaskManager.record_task_metadata = true
Rake.application = rake
rake.init
rake.load_rakefile
Rake.application.tasks.each do |t, n|
puts t
puts t.full_comment
puts "\n"
end
Use the methods comment or full_comment for that. More docs on the Rake::Task class here.

How to access namespace from within a rake task?

My rake task (rake v 0.9.2.2) includes a bit to print out the name of the task. I'd like it to also print its namespace. Kind of like the following:
namespace :yelp do
desc "..."
task :scrape => :environment do
puts "rake #{task.namespace}:#{task.name}"
end
end
The problem is that namespace is a private method.
rake aborted!
private method `namespace' called for <Rake::Task => []>:Rake::Task
Anyone have a solution?
task.name includes the namespace. Use this tip to get task.name to print under rake 0.9.2.2.

Generate migrations outside Rails

I'm using ActiveRecord outside Rails. I would want a program to generate the skeleton of a migration ( as well as a system to collect and maintain them ).
Can anyone make a suggestion?
Also take a look at new active_record_migrations
There is a gem to use Rails Database Migrations in non Rails projects. Its name is "standalone_migrations"
Here is a link
https://github.com/thuss/standalone-migrations
If you do not like to use rake, but still get the system part of ActiveRecord::Migration, then you can use the following to handle the ups and downs from plain ruby (without any rails):
require 'active_record'
require 'benchmark'
# Migration method, which does not uses files in db/migrate but in-memory migrations
# Based on ActiveRecord::Migrator::migrate
def migrate(migrations, target_version = nil)
direction = case
when target_version.nil?
:up
when (ActiveRecord::Migrator::current_version == target_version)
return # do nothing
when ActiveRecord::Migrator::current_version > target_version
:down
else
:up
end
ActiveRecord::Migrator.new(direction, migrations, target_version).migrate
puts "Current version: #{ActiveRecord::Migrator::current_version}"
end
# MigrationProxy deals with loading Migrations from files, we reuse it
# to create instances of the migration classes we provide
class MigrationClassProxy < ActiveRecord::MigrationProxy
def initialize(migrationClass, version)
super(migrationClass.name, version, nil, nil)
#migrationClass = migrationClass
end
def mtime
0
end
def load_migration
#migrationClass.new(name, version)
end
end
# Hash of all our migrations
migrations = {
2016_08_09_2013_00 =>
class CreateSolutionTable < ActiveRecord::Migration[5.0]
def change
create_table :solution_submissions do |t|
t.string :problem_hash, index: true
t.string :solution_hash, index: true
t.float :resemblance
t.timestamps
end
end
self # Necessary to get the class instance into the hash!
end,
2016_08_09_2014_16 =>
class CreateProductFields < ActiveRecord::Migration[5.0]
# ...
self
end
}.map { |key,value| MigrationClassProxy.new(value, key) }
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'XXX.db'
)
# Play all migrations (rake db:migrate)
migrate(migrations, migrations.last.version)
# ... or undo them (rake db:migrate VERSION=0)
migrate(migrations, 0)
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class SolutionSubmission < ApplicationRecord
end
I have made a minimal example of how to use active record outside of Rails. Rails migrations (Standalone migrations) in non-Rails (and non Ruby) projects.
https://github.com/euclid1990/rails-migration
(Support Rails >= 5.2)
You can refer Rake file in this repo.
There is another gem called otr-activerecord. This gem provides following tasks:
rake db:create
rake db:create_migration[name]
rake db:drop
rake db:environment:set
rake db:fixtures:load
rake db:migrate
rake db:migrate:status
rake db:rollback
rake db:schema:cache:clear
rake db:schema:cache:dump
rake db:schema:dump
rake db:schema:load
rake db:seed
rake db:setup
rake db:structure:dump
rake db:structure:load
rake db:version
All you need to do is to install it and add a Rakefile with content
load 'tasks/otr-activerecord.rake'
OTR::ActiveRecord.configure_from_file! 'config/database.yml'
I prefer this gem over active_record_migrations or Standalone Migration because those two gems depend on railties, which is almost the entire Rails. For example, Nokogiri takes a long time to compile, and takes a lot of spaces.

How do I log queries in a rake task using datamapper and padrino?

I have a padrino install that is using datamapper and logging queries to a file. This is working fine when browsing my application. But queries are not logged if executed inside a rake file. Why?
This is how the task is defined:
# lib/tasks/example.rake
task :example => :environment do
players = Player.all #Player is a datamapper object
puts players.first.to_s
end
I also added this line to /config/boot.rb
Padrino::Logger::Config[:development] = { :log_level => :devel, :stream => :to_file }
And this line is called in /config/database.rb
DataMapper.logger = logger
And this is how I'm executing the script
$ padrino rake example
Invoke rake with:
PADRINO_LOG_LEVEL=development padrino rake my:task

Resources