Listing posts

Displaying posts 276 - 280 of 328 in total
Git guides and workflows attachment
mouse 1839 · person cloud · link
Last update
2017-07-26
2017
07-26
« — »

~~~ * ~~~

Rails | Temporary connection to another DB
mouse 1886 · person cloud · link
Last update
2017-07-26
2017
07-26
« — »

All code in the given block will execute on the specified connection:

1
2
3
4
5
6
7
8
9
class MyRecord < ActiveRecord::Base
  def self.with_db(db_config)
    orig_conn = self.remove_connection
    self.establish_connection db_config
    yield if block_given?
  ensure
    self.establish_connection orig_conn || self.configurations[Rails.env]
  end
end

Source: StackOverflow


~~~ * ~~~

Rails multiple databases, migrations and transactions
mouse 3486 · person cloud · link
Last update
2017-07-26
2017
07-26
« — »

Start by configuring your rails app for multiple DBs:

1
2
3
4
5
6
7
8
9
10
11
12
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

production:    { <<: *default, database: db/production.sqlite3       }
development:   { <<: *default, database: db/development.sqlite3      }

# additional DB
:cash:
  production:  { <<: *default, database: db/production_cash.sqlite3  }
  development: { <<: *default, database: db/development_cash.sqlite3 }

Share a common connection between desired models (in the example I use namespaced models1):

1
2
3
4
5
6
7
# /app/models/cash/shared_base.rb
class Cash::SharedBase < ActiveRecord::Base
  establish_connection configurations[:cash][Rails.env]
  self.abstract_class = true
end
class Cash::Account  < Cash::SharedBase; ...; end # /app/models/cash/account.rb
class Cash::Movement < Cash::SharedBase; ...; end # /app/models/cash/movement.rb

Modify your migrations so they can use your custom connection and save the current schema version to the main DB:

1
2
3
4
5
# /db/migrate/XXXXXXXXXXXXXXXX_create_cash_accounts.rb
class CreateCashAccounts < ActiveRecord::Migration
  def connection; Cash::SharedBase.connection; end
  def change; ...; end
end

downside note: migration rollback will not work.

If you want to create a unique db/schema.rb dump, you can use this simple rake task:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# /lib/tasks/db.rake
namespace :db do
  desc 'Dump a unique schema for all databases'
  task 'schema:dump_all': :environment do
    def dump_conn(c)
      f = StringIO.new
      ActiveRecord::SchemaDumper.dump c, f # https://github.com/rails/rails/issues/3497#issuecomment-5419288
      f.string.split("\n")
    end

    # dump all schemas in memory
    arb      = ActiveRecord::Base
    schemas  = [ dump_conn(arb.connection) ]
    schemas += arb.configurations.keys.reject{|k| k.is_a? String }.
      map{|db| dump_conn arb.establish_connection(arb.configurations[db][Rails.env]).connection }

    # join schemas
    lines = []
    schemas.each_with_index do |schema, i|
      lines += schema.grep(/^ActiveRecord::Schema/) if lines.empty? # save header
      lines += schema.grep(/^\s/)                                   # append rows
      lines << "\n  # #{'-'*76}\n" if (i+1) < schemas.size          # separator
    end
    lines << 'end' # close block

    File.open(Rails.root.join('db/schema.rb').to_s, 'w'){|f| f.puts lines.join("\n") }
  end # dump_all

  # ----------------------------------------------------------------------------

  Rake::Task["db:schema:dump"].enhance do  # hook into existing task
    Rake::Task["db:schema:dump_all"].invoke
  end
end

Enable ActiveRecord's query cache for the new connections:

1
2
3
4
5
6
7
8
9
10
11
12
class ApplicationController < ActionController::Base
  around_action :cache_other_db_connections

  private

  def cache_other_db_connections
    Cash::SharedBase.connection.cache { yield }
    # if you have more connections make more around filters of just nest the calls:
    # M1.connection.cache{ M2.connection.cache { ... {yield} } }
  end
end

And last but not least, rails uses a transaction for each create/update so pay attention to multi-DB transactions as the ActiveRecord::Rollback exception does not propagate and thus it will not rollback the outer transaction if raised within an inner transaction (read Rails Multi-Database Best Practices Roundup for a more in depth explanation).


References:


  1. You can generate namespaced models with the standard rails generator: rails g model NamespaceName::ModelName ...


~~~ * ~~~

KAKASI | japanese to romaji converter
mouse 1851 · person cloud · link
Last update
2017-07-26
2017
07-26
« — »
1
2
3
4
5
6
7
8
9
10
aptitude install kakasi

cat document.txt | \
  kakasi -Ha -Ka -Ja -Ea -ka -ja -ga \  # convert all to ascii/romaji
         -s              \  # insert space between words
         -c              \  # skip special chars (TAB,CR,LF,BLANK)
         -i utf8 -o utf8 \  # input/output charset
         -rhepburn       \  # hepburn translation method
         -p              \  # display all possibile readings
         -f                 # show original kanji+translation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gem 'kakasi' # add line to Gemfile

# make a useful string method
module Utils::String
  KAKASI_OPTS = %w{ -Ha -Ka -Ja -Ea -ka -ja -ga -s -c -rhepburn }

  def to_romaji(kakasi_opts = [])
    enc_opts = {invalid: :replace, undef: :replace, replace: '_'}

    Kakasi.kakasi(
      (KAKASI_OPTS+kakasi_opts).join(' '),
      self.encode(Encoding::CP932, enc_opts) # fix UTF-8 to Windows-31J (Encoding::UndefinedConversionError)
    ).encode(Encoding::UTF_8, enc_opts) # reencode to UTF8 for further processing
  end # to_romaji --------------------------------------------------------------
end

String.send(:include, ::Utils::String)

Source: KAKASI Hp, manpage, ruby gem


~~~ * ~~~

Zuccotto col pandoro
mouse 1647 · person cloud · link
Last update
2017-07-13
2017
07-13
« — »
Cover original

Ingredienti:

  • 500ml panna fresca da montare
  • 250gr mascarpone
  • 100gr amaretti/torrone friabile (optional)
  • 1x pandoro
  • 1x bicchiere di latte
  • 5x cucchiai zucchero a velo

Accessori:

  • 1x terrina
  • pellicola (domopak)

Procedimento:

  1. Affettare il pandoro (~1,5cm) e inzuppare le fette nel latte dalla più piccola in su.

  2. Base: foderare la terrina con la pellicola e poi con le fette, e mettere in freezer per 10 minuti.

  3. Crema: amalgamare mascarpone, zucchero a velo, la panna montata a neve, e a piacere gli amaretti/torrone sbriciolati.

  4. Mettere metà crema nella base, tapparla col resto delle fette e uno strato di pellicola, e mettete in freezer per 20 minuti.

  5. Ricoprire col resto della crema e decorare con altre briciole.


Fonte: GialloZafferano, Cucchiaio