File: Rakefile

package info (click to toggle)
ruby-otr-activerecord 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 356 kB
  • sloc: ruby: 561; sh: 133; makefile: 6
file content (48 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download | duplicates (3)
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
require 'bundler/setup'
load 'tasks/otr-activerecord.rake'

OTR::ActiveRecord.db_dir = 'data'
OTR::ActiveRecord.migrations_paths = ['data/migrate']
OTR::ActiveRecord.seed_file = 'seeds.rb'

namespace :db do
  task :environment do
    require_relative 'environment'
  end
end

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

  desc "Fetch a single widget"
  task :fetch, [:id] => 'db:environment' do |_, args|
    widget = Widget.find(args[:id])
    puts widget.to_json
  end

  desc "Create a new widget"
  task :create, [:name] => 'db:environment' do |_, args|
    widget = Widget.new(name: args[:name])
    if widget.save
      puts widget.to_json
    else
      abort({errors: widget.errors.full_message}.to_json)
    end
  end

  desc "Update a widget"
  task :update, [:id, :name] => 'db:environment' do |_, args|
    widget = Widget.where(id: args[:id]).first
    abort({errors: ["Widget '#{args[:id]}' not found"]}.to_json) if widget.nil?

    if widget.update_columns({name: args[:name]})
      puts widget.to_json
    else
      abort({errors: widget.errors.full_message}.to_json)
    end
  end
end