mobb-activerecord 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97e535192c6d00e1b58c3bd6e4133986a68419894c487d878f2255660f3e5b98
4
+ data.tar.gz: 4e5de1f724c0db7b6f8157402818d5553ee5301ebd607a30355daaf4d247122e
5
+ SHA512:
6
+ metadata.gz: 7c367414c5cfca87b0180888e6180d1f1e4a9836ebad93864f59029c7945f38f25d3b1aa2221b87ff892e0ea70e7bdf117beb026171368ee2a7656f6a4261415
7
+ data.tar.gz: f125319e3a6b1ad737fbcc5a1dbb6a4686456115320e54ebfc4cb2b40e2d47e343120caf161e89b0732d48ef6e260d1bb99c04221578f845c87368f9e99fa73b
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018 kinoppyd
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,150 @@
1
+ # Mobb ActiveRecord Extension
2
+
3
+ Extends [Mobb](https://github.com/kinoppyd/mobb) with extension methods and Rake
4
+ tasks for dealing with an SQL database using the
5
+ [ActiveRecord ORM](https://github.com/rails/rails/tree/master/activerecord).
6
+
7
+ ## Setup
8
+
9
+ Put it in your `Gemfile`, along with the adapter of your database. For
10
+ simplicity, let's assume you're using SQLite:
11
+
12
+ ```ruby
13
+ gem "mobb-activerecord"
14
+ gem "sqlite3"
15
+ gem "rake"
16
+ ```
17
+
18
+ Now require it in your Mobb application, and establish the database
19
+ connection:
20
+
21
+ ```ruby
22
+ # app.rb
23
+ require "mobb/activerecord"
24
+
25
+ set :database, {adapter: "sqlite3", database: "foo.sqlite3"}
26
+ # or set :database_file, "path/to/database.yml"
27
+ ```
28
+
29
+ If you have a `config/database.yml`, it will automatically be loaded, no need
30
+ to specify it. Also, in production, the `$DATABASE_URL` environment variable
31
+ will automatically be read as the database (if you haven't specified otherwise).
32
+
33
+ Note that in **modular** Mobb applications you will need to first register
34
+ the extension:
35
+
36
+ ```ruby
37
+ class YourApplication < Mobb::Base
38
+ register Mobb::ActiveRecordExtension
39
+ end
40
+ ```
41
+
42
+ Now require the rake tasks and your app in your `Rakefile`:
43
+
44
+ ```ruby
45
+ # Rakefile
46
+ require "mobb/activerecord/rake"
47
+
48
+ namespace :db do
49
+ task :load_config do
50
+ require "./app"
51
+ end
52
+ end
53
+ ```
54
+
55
+ In the Terminal test that it works:
56
+
57
+ ```sh
58
+ $ bundle exec rake -T
59
+ rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
60
+ rake db:create_migration # Create a migration (parameters: NAME, VERSION)
61
+ rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)
62
+ rake db:fixtures:load # Load fixtures into the current environment's database
63
+ rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false)
64
+ rake db:migrate:status # Display status of migrations
65
+ rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
66
+ rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
67
+ rake db:schema:load # Load a schema.rb file into the database
68
+ rake db:seed # Load the seed data from db/seeds.rb
69
+ rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
70
+ rake db:structure:dump # Dump the database structure to db/structure.sql
71
+ rake db:version # Retrieves the current schema version number
72
+ ```
73
+
74
+ And that's it, you're all set :)
75
+
76
+ ## Usage
77
+
78
+ You can create a migration:
79
+
80
+ ```sh
81
+ $ bundle exec rake db:create_migration NAME=create_users
82
+ ```
83
+
84
+ This will create a migration file in your migrations directory (`./db/migrate`
85
+ by default), ready for editing.
86
+
87
+ ```ruby
88
+ class CreateUsers < ActiveRecord::Migration
89
+ def change
90
+ create_table :users do |t|
91
+ t.string :name
92
+ end
93
+ end
94
+ end
95
+ ```
96
+
97
+ Now migrate the database:
98
+
99
+ ```sh
100
+ $ bundle exec rake db:migrate
101
+ ```
102
+
103
+ You can also write models:
104
+
105
+ ```ruby
106
+ class User < ActiveRecord::Base
107
+ validates_presence_of :name
108
+ end
109
+ ```
110
+
111
+ You can put your models anywhere you want, only remember to require them if
112
+ they're in a separate file, and that they're loaded after `require "mobb/activerecord"`.
113
+
114
+ Now everything just works:
115
+
116
+ ```ruby
117
+ on 'list users' do
118
+ users = User.all
119
+ users.map{ |u| "- #{u.name}"}.join("\n")
120
+ end
121
+
122
+ on /user (\w+)/ do |name|
123
+ user = User.find_by(name: name)
124
+ "#{user.id} : #{user.name}"
125
+ end
126
+ ```
127
+
128
+ A nice thing is that the `ActiveRecord::Base` class is available to
129
+ you through the `database` variable:
130
+
131
+ ```ruby
132
+ if database.table_exists?('users')
133
+ # Do stuff
134
+ else
135
+ raise "The table 'users' doesn't exist."
136
+ end
137
+ ```
138
+
139
+ ## History
140
+
141
+ This gem was made in 2009 by Blake Mizerany, creator of Sinatra.
142
+ And forked by Janko Marohnić to improve on.
143
+
144
+ ## Social
145
+
146
+ You can follow me on Twitter, I'm [@GhostBrain](http://twitter.com/GhostBrain).
147
+
148
+ ## License
149
+
150
+ [MIT](https://github.com/kinoppyd/mobb-activerecord/blob/master/LICENSE)
@@ -0,0 +1,62 @@
1
+ require 'mobb/base'
2
+ require 'active_record'
3
+ require 'active_support/core_ext/hash/keys'
4
+
5
+ require 'logger'
6
+ require 'pathname'
7
+ require 'yaml'
8
+ require 'erb'
9
+
10
+ module Mobb
11
+ module ActiveRecordHelper
12
+ def database
13
+ settings.database
14
+ end
15
+ end
16
+
17
+ module ActiveRecordExtension
18
+ def self.registered(app)
19
+ if ENV['DATABASE_URL']
20
+ app.set :database, ENV['DATABASE_URL']
21
+ elsif File.exist?("#{Dir.pwd}/config/database.yml")
22
+ app.set :database_file, "#{Dir.pwd}/config/database.yml"
23
+ end
24
+
25
+ unless defined?(Rake) || [:test, :production].include?(app.settings.environment)
26
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
27
+ end
28
+
29
+ app.helpers ActiveRecordHelper
30
+
31
+ # re-connect if database connection dropped (Rails 3 only)
32
+ app.before { ActiveRecord::Base.verify_active_connections! if ActiveRecord::Base.respond_to?(:verify_active_connections!) }
33
+ app.after { ActiveRecord::Base.clear_active_connections! }
34
+ end
35
+
36
+ def database_file=(path)
37
+ path = File.join(root, path) if Pathname(path).relative? and root
38
+ spec = YAML.load(ERB.new(File.read(path)).result) || {}
39
+ set :database, spec
40
+ end
41
+
42
+ def database=(spec)
43
+ if spec.is_a?(Hash) and spec.symbolize_keys[environment.to_sym]
44
+ ActiveRecord::Base.configurations = spec.stringify_keys
45
+ ActiveRecord::Base.establish_connection(environment.to_sym)
46
+ elsif spec.is_a?(Hash)
47
+ ActiveRecord::Base.configurations[environment.to_s] = spec.stringify_keys
48
+ ActiveRecord::Base.establish_connection(spec.stringify_keys)
49
+ else
50
+ ActiveRecord::Base.establish_connection(spec)
51
+ ActiveRecord::Base.configurations ||= {}
52
+ ActiveRecord::Base.configurations[environment.to_s] = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(spec).to_hash
53
+ end
54
+ end
55
+
56
+ def database
57
+ ActiveRecord::Base
58
+ end
59
+ end
60
+
61
+ register ActiveRecordExtension
62
+ end
@@ -0,0 +1,6 @@
1
+ load "active_record/railties/databases.rake"
2
+ require "mobb/activerecord/rake/activerecord_#{ActiveRecord::VERSION::MAJOR}"
3
+
4
+ load "mobb/activerecord/tasks.rake"
5
+
6
+ ActiveRecord::Base.logger = nil
@@ -0,0 +1,31 @@
1
+ require "active_support/string_inquirer"
2
+
3
+ module Rails
4
+ extend self
5
+
6
+ def root
7
+ Pathname.new(Rake.application.original_dir)
8
+ end
9
+
10
+ def env
11
+ ActiveSupport::StringInquirer.new(ENV["REPP_ENV"] || "development")
12
+ end
13
+
14
+ def application
15
+ seed_loader = Object.new
16
+ seed_loader.instance_eval do
17
+ def load_seed
18
+ load "db/seeds.rb"
19
+ end
20
+ end
21
+ seed_loader
22
+ end
23
+ end
24
+
25
+ # db:load_config can be overriden manually
26
+ Rake::Task["db:seed"].enhance(["db:load_config"])
27
+ Rake::Task["db:load_config"].clear
28
+
29
+ # define Rails' tasks as no-op
30
+ Rake::Task.define_task("db:environment")
31
+ Rake::Task.define_task("db:rails_env")
@@ -0,0 +1,23 @@
1
+ seed_loader = Class.new do
2
+ def load_seed
3
+ load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/seeds.rb"
4
+ end
5
+ end
6
+
7
+ ActiveRecord::Tasks::DatabaseTasks.tap do |config|
8
+ config.root = Rake.application.original_dir
9
+ config.env = ENV["REPP_ENV"] || "development"
10
+ config.db_dir = "db"
11
+ config.migrations_paths = ["db/migrate"]
12
+ config.fixtures_path = "test/fixtures"
13
+ config.seed_loader = seed_loader.new
14
+ config.database_configuration = ActiveRecord::Base.configurations
15
+ end
16
+
17
+ # db:load_config can be overriden manually
18
+ Rake::Task["db:seed"].enhance(["db:load_config"])
19
+ Rake::Task["db:load_config"].clear
20
+
21
+ # define Rails' tasks as no-op
22
+ Rake::Task.define_task("db:environment")
23
+ Rake::Task["db:test:deprecated"].clear if Rake::Task.task_defined?("db:test:deprecated")
@@ -0,0 +1,23 @@
1
+ seed_loader = Class.new do
2
+ def load_seed
3
+ load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/seeds.rb"
4
+ end
5
+ end
6
+
7
+ ActiveRecord::Tasks::DatabaseTasks.tap do |config|
8
+ config.root = Rake.application.original_dir
9
+ config.env = ENV["REPP_ENV"] || "development"
10
+ config.db_dir = "db"
11
+ config.migrations_paths = ["db/migrate"]
12
+ config.fixtures_path = "test/fixtures"
13
+ config.seed_loader = seed_loader.new
14
+ config.database_configuration = ActiveRecord::Base.configurations
15
+ end
16
+
17
+ # db:load_config can be overriden manually
18
+ Rake::Task["db:seed"].enhance(["db:load_config"])
19
+ Rake::Task["db:load_config"].clear
20
+
21
+ # define Rails' tasks as no-op
22
+ Rake::Task.define_task("db:environment")
23
+ Rake::Task["db:test:deprecated"].clear if Rake::Task.task_defined?("db:test:deprecated")
@@ -0,0 +1,52 @@
1
+ require "active_support/core_ext/string/strip"
2
+ require "pathname"
3
+ require "fileutils"
4
+
5
+ namespace :db do
6
+ desc "Create a migration (parameters: NAME, VERSION)"
7
+ task :create_migration do
8
+ unless ENV["NAME"]
9
+ puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`"
10
+ exit
11
+ end
12
+
13
+ name = ENV["NAME"]
14
+ version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
15
+
16
+ ActiveRecord::Migrator.migrations_paths.each do |directory|
17
+ next unless File.exist?(directory)
18
+ migration_files = Pathname(directory).children
19
+ if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
20
+ puts "Another migration is already named \"#{name}\": #{duplicate}."
21
+ exit
22
+ end
23
+ end
24
+
25
+ filename = "#{version}_#{name}.rb"
26
+ dirname = ActiveRecord::Migrator.migrations_paths.first
27
+ path = File.join(dirname, filename)
28
+ ar_maj = ActiveRecord::VERSION::MAJOR
29
+ ar_min = ActiveRecord::VERSION::MINOR
30
+ base = "ActiveRecord::Migration"
31
+ base += "[#{ar_maj}.#{ar_min}]" if ar_maj >= 5
32
+
33
+ FileUtils.mkdir_p(dirname)
34
+ File.write path, <<-MIGRATION.strip_heredoc
35
+ class #{name.camelize} < #{base}
36
+ def change
37
+ end
38
+ end
39
+ MIGRATION
40
+
41
+ puts path
42
+ end
43
+ end
44
+
45
+ # The `db:create` and `db:drop` command won't work with a DATABASE_URL because
46
+ # the `db:load_config` command tries to connect to the DATABASE_URL, which either
47
+ # doesn't exist or isn't able to drop the database. Ignore loading the configs for
48
+ # these tasks if a `DATABASE_URL` is present.
49
+ if ENV.has_key? "DATABASE_URL"
50
+ Rake::Task["db:create"].prerequisites.delete("load_config")
51
+ Rake::Task["db:drop"].prerequisites.delete("load_config")
52
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mobb-activerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Blake Mizerany
8
+ - Janko Marohnić
9
+ - kinoppyd
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2018-11-01 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mobb
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0.4'
29
+ - !ruby/object:Gem::Dependency
30
+ name: activerecord
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '3.2'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '3.2'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '3.1'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '3.1'
71
+ - !ruby/object:Gem::Dependency
72
+ name: sqlite3
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: appraisal
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ description: Extends mobb with ActiveRecord helpers.
100
+ email: WhoIsDissolvedGirl+github@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - LICENSE
106
+ - README.md
107
+ - lib/mobb/activerecord.rb
108
+ - lib/mobb/activerecord/rake.rb
109
+ - lib/mobb/activerecord/rake/activerecord_3.rb
110
+ - lib/mobb/activerecord/rake/activerecord_4.rb
111
+ - lib/mobb/activerecord/rake/activerecord_5.rb
112
+ - lib/mobb/activerecord/tasks.rake
113
+ homepage: http://github.com/kinoppyd/mobb-activerecord
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 1.9.2
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.7.6
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Extends mobb with ActiveRecord helpers.
137
+ test_files: []