File: test_app.rb

package info (click to toggle)
ruby-otr-activerecord 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 356 kB
  • sloc: ruby: 561; sh: 133; makefile: 6
file content (100 lines) | stat: -rw-r--r-- 2,389 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'fileutils'

module TestApp
  DIR = "/tmp/otr-activerecord-test-app"
  DB_DEV = "db/development.sqlite3"
  DB_TEST = "db/test.sqlite3"
  DB_PROD = "db/production.sqlite3"

  def self.delete
    FileUtils.rm_r DIR
  end

  def self.exists?
    Dir.exist? DIR
  end

  def self.create
    FileUtils.mkdir_p DIR
    FileUtils.mkdir_p File.join(DIR, "db", "migrate")
    File.write File.join(DIR, "Gemfile"), gemfile
    File.write File.join(DIR, "Rakefile"), rakefile
    File.write File.join(DIR, "models.rb"), models
    File.write File.join(DIR, "db", "schema.rb"), schemarb
    File.write File.join(DIR, "db", "seeds.rb"), seedsrb
    File.write File.join(DIR, "db", "config.yaml"), dbconfig
  end

  def self.models
    <<-STR
      class Widget < ActiveRecord::Base
      end
    STR
  end

  def self.gemfile
    File
      .readlines(File.join("gemfiles", "ar_#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}.gemfile"))
      .select { |line| line =~ /source|rake|activerecord|sqlite/ }
      .push("gem 'otr-activerecord', path: #{Rake.application.original_dir}")
      .join("")
  end

  def self.rakefile
    <<-STR
      require 'json'
      require 'bundler/setup'
      load 'tasks/otr-activerecord.rake'

      namespace :db do
        task :environment do
          require_relative "./models"
          OTR::ActiveRecord.configure_from_file! "db/config.yaml"
          OTR::ActiveRecord.establish_connection!
        end
      end

      namespace :widgets do
        desc "List all widgets"
        task :list => 'db:environment' do
          widgets = Widget.order('name').to_a
          puts widgets.map(&:as_json).to_json
        end
      end
    STR
  end

  def self.dbconfig
    <<-STR
      development:
        adapter: sqlite3
        database: #{DB_DEV}

      test:
        adapter: sqlite3
        database: #{DB_TEST}

      production:
        adapter: sqlite3
        database: #{DB_PROD}
    STR
  end

  def self.schemarb
    <<-STR
      ActiveRecord::Schema.define(version: 2026_07_02_013351) do
        create_table "widgets", force: :cascade do |t|
          t.string "name", null: false
          t.datetime "created_at", precision: 6, null: false
          t.datetime "updated_at", precision: 6, null: false
        end
      end
    STR
  end

  def self.seedsrb
    <<-STR
      Widget.create!(name: "Foo")
    STR
  end
end