-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Josh Bassett
committed
Mar 14, 2017
1 parent
6c6025b
commit 6c1e54e
Showing
18 changed files
with
230 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
*.gem | ||
.bundle | ||
coverage | ||
.yardoc | ||
Gemfile.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
AllCops: | ||
Exclude: | ||
- "bin/**/*" | ||
- "mache.gemspec" | ||
- "rein.gemspec" | ||
|
||
Metrics/AbcSize: | ||
Max: 27 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
require "rubocop/rake_task" | ||
|
||
RSpec::Core::RakeTask.new | ||
RSpec::Core::RakeTask.new(:spec) | ||
RuboCop::RakeTask.new | ||
|
||
task default: :spec | ||
task default: [:spec, :rubocop] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,64 @@ | ||
module RC | ||
module ForeignKey | ||
def add_foreign_key_constraint(referencing_table, referenced_table, options = {}) | ||
referencing_attribute = (options[:referencing] || "#{referenced_table.to_s.singularize}_id").to_sym | ||
referenced_attribute = (options[:referenced] || "id").to_sym | ||
module Rein | ||
module Constraint | ||
# This module contains methods for defining foreign key constraints. | ||
module ForeignKey | ||
def add_foreign_key_constraint(referencing_table, referenced_table, options = {}) | ||
referencing_attribute = (options[:referencing] || "#{referenced_table.to_s.singularize}_id").to_sym | ||
referenced_attribute = (options[:referenced] || "id").to_sym | ||
|
||
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute) | ||
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute) | ||
|
||
sql = "ALTER TABLE #{referencing_table}".tap do |sql| | ||
sql = "ALTER TABLE #{referencing_table}" | ||
sql << " ADD CONSTRAINT #{name}" | ||
sql << " FOREIGN KEY (#{referencing_attribute})" | ||
sql << " REFERENCES #{referenced_table} (#{referenced_attribute})" | ||
sql << " ON DELETE #{referential_action(options[:on_delete])}" if options[:on_delete].present? | ||
sql << " ON UPDATE #{referential_action(options[:on_update])}" if options[:on_update].present? | ||
end | ||
|
||
execute(sql) | ||
execute(sql) | ||
|
||
# A foreign key constraint doesn't have an implicit index. | ||
add_index(referencing_table, referencing_attribute) if options[:add_index] == true | ||
end | ||
alias_method :add_foreign_key, :add_foreign_key_constraint | ||
# A foreign key constraint doesn't have an implicit index. | ||
add_index(referencing_table, referencing_attribute) if options[:add_index] == true | ||
end | ||
alias add_foreign_key add_foreign_key_constraint | ||
|
||
def remove_foreign_key_constraint(referencing_table, referenced_table, options = {}) | ||
referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id".to_sym | ||
def remove_foreign_key_constraint(referencing_table, referenced_table, options = {}) | ||
referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id".to_sym | ||
|
||
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute) | ||
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute) | ||
|
||
if is_a_mysql_adapter? | ||
execute "ALTER TABLE #{referencing_table} DROP FOREIGN KEY #{name}" | ||
else | ||
execute "ALTER TABLE #{referencing_table} DROP CONSTRAINT #{name}" | ||
if mysql_adapter? | ||
execute "ALTER TABLE #{referencing_table} DROP FOREIGN KEY #{name}" | ||
else | ||
execute "ALTER TABLE #{referencing_table} DROP CONSTRAINT #{name}" | ||
end | ||
|
||
# A foreign key constraint doesn't have an implicit index. | ||
remove_index(referencing_table, referencing_attribute) if options[:remove_index] == true | ||
end | ||
alias remove_foreign_key remove_foreign_key_constraint | ||
|
||
# A foreign key constraint doesn't have an implicit index. | ||
remove_index(referencing_table, referencing_attribute) if options[:remove_index] == true | ||
end | ||
alias_method :remove_foreign_key, :remove_foreign_key_constraint | ||
private | ||
|
||
private | ||
def referential_action(action) | ||
case action.to_sym | ||
when :no_action | ||
"NO ACTION" | ||
when :cascade | ||
"CASCADE" | ||
when :restrict | ||
"RESTRICT" | ||
when :nullify | ||
"SET NULL" | ||
when :default | ||
"SET DEFAULT" | ||
else | ||
raise "Unknown referential action '#{action}'" | ||
def referential_action(action) | ||
case action.to_sym | ||
when :no_action then "NO ACTION" | ||
when :cascade then "CASCADE" | ||
when :restrict then "RESTRICT" | ||
when :nullify then "SET NULL" | ||
when :default then "SET DEFAULT" | ||
else | ||
raise "Unknown referential action '#{action}'" | ||
end | ||
end | ||
end | ||
|
||
def is_a_mysql_adapter? | ||
self.class.to_s =~ /Mysql[2]?Adapter/ | ||
end | ||
def mysql_adapter? | ||
self.class.to_s =~ /Mysql[2]?Adapter/ | ||
end | ||
|
||
def default_constraint_name(referencing_table, referencing_attribute) | ||
"#{referencing_table}_#{referencing_attribute}_fk".to_sym | ||
def default_constraint_name(referencing_table, referencing_attribute) | ||
"#{referencing_table}_#{referencing_attribute}_fk".to_sym | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
require 'active_record/connection_adapters/abstract/quoting' | ||
require "active_record/connection_adapters/abstract/quoting" | ||
|
||
module RC | ||
module Inclusion | ||
include ActiveRecord::ConnectionAdapters::Quoting | ||
module Rein | ||
module Constraint | ||
# This module contains methods for defining inclusion constraints. | ||
module Inclusion | ||
include ActiveRecord::ConnectionAdapters::Quoting | ||
|
||
def add_inclusion_constraint(table, attribute, options = {}) | ||
name = "#{table}_#{attribute}" | ||
values = options[:in].map {|value| quote(value) }.join(", ") | ||
conditions = "#{attribute} IN (#{values})" | ||
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})") | ||
def add_inclusion_constraint(table, attribute, options = {}) | ||
name = "#{table}_#{attribute}" | ||
values = options[:in].map { |value| quote(value) }.join(", ") | ||
conditions = "#{attribute} IN (#{values})" | ||
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
module RC | ||
module Numericality | ||
OPERATORS = { | ||
:greater_than => :>, | ||
:greater_than_or_equal_to => :>=, | ||
:equal_to => :"=", | ||
:not_equal_to => :"!=", | ||
:less_than => :<, | ||
:less_than_or_equal_to => :<= | ||
}.freeze | ||
module Rein | ||
module Constraint | ||
# This module contains methods for defining numericality constraints. | ||
module Numericality | ||
OPERATORS = { | ||
greater_than: :>, | ||
greater_than_or_equal_to: :>=, | ||
equal_to: :"=", | ||
not_equal_to: :"!=", | ||
less_than: :<, | ||
less_than_or_equal_to: :<= | ||
}.freeze | ||
|
||
def add_numericality_constraint(table, attribute, options = {}) | ||
name = "#{table}_#{attribute}" | ||
def add_numericality_constraint(table, attribute, options = {}) | ||
name = "#{table}_#{attribute}" | ||
|
||
conditions = OPERATORS.slice(*options.keys).map do |key, operator| | ||
value = options[key] | ||
[attribute, operator, value].join(" ") | ||
end.join(" AND ") | ||
conditions = OPERATORS.slice(*options.keys).map do |key, operator| | ||
value = options[key] | ||
[attribute, operator, value].join(" ") | ||
end.join(" AND ") | ||
|
||
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})") | ||
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
module RC | ||
module Presence | ||
include ActiveRecord::ConnectionAdapters::Quoting | ||
module Rein | ||
module Constraint | ||
# This module contains methods for defining presence constraints. | ||
module Presence | ||
include ActiveRecord::ConnectionAdapters::Quoting | ||
|
||
def add_presence_constraint(table, attribute) | ||
name = "#{table}_#{attribute}" | ||
conditions = "#{attribute} !~ '^\s*$'" | ||
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})") | ||
def add_presence_constraint(table, attribute) | ||
name = "#{table}_#{attribute}" | ||
conditions = "#{attribute} !~ '^\s*$'" | ||
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
module RC | ||
module PrimaryKey | ||
def add_primary_key(table, options = {}) | ||
attribute = (options[:column] || "id").to_sym | ||
sql = "ALTER TABLE #{table} ADD PRIMARY KEY (#{attribute})" | ||
execute(sql) | ||
module Rein | ||
module Constraint | ||
# This module contains methods for defining primary key constraints. | ||
module PrimaryKey | ||
def add_primary_key(table, options = {}) | ||
attribute = (options[:column] || "id").to_sym | ||
sql = "ALTER TABLE #{table} ADD PRIMARY KEY (#{attribute})" | ||
execute(sql) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
$:.push File.expand_path("../lib", __FILE__) | ||
lib = File.expand_path("../lib", __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
|
||
require "rein/version" | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "rein" | ||
s.version = Rein::VERSION | ||
s.author = "Josh Bassett" | ||
s.email = "[email protected]" | ||
s.homepage = "http://github.com/nullobject/rein" | ||
s.summary = "Database constraints made easy for ActiveRecord." | ||
s.description = "Rein adds bunch of methods to your ActiveRecord migrations so you can easily tame your database." | ||
Gem::Specification.new do |spec| | ||
spec.name = "rein" | ||
spec.version = Rein::VERSION | ||
spec.author = "Joshua Bassett" | ||
spec.email = "[email protected]" | ||
spec.summary = "Database constraints made easy for ActiveRecord." | ||
spec.description = "Rein adds bunch of methods to your ActiveRecord migrations so you can easily tame your database." | ||
spec.homepage = "http://github.com/nullobject/rein" | ||
spec.license = "MIT" | ||
spec.files = `git ls-files -z`.split("\x0").reject do |f| | ||
f.match(%r{^(test|spec|features)/}) | ||
end | ||
spec.require_paths = ["lib"] | ||
|
||
s.rubyforge_project = "rein" | ||
spec.add_runtime_dependency "activerecord", ">= 3.2.0" | ||
|
||
s.files = `git ls-files`.split("\n") | ||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
s.executables = `git ls-files -- bin/*`.split("\n").map {|f| File.basename(f) } | ||
|
||
s.add_development_dependency "rake" | ||
s.add_development_dependency "rspec" | ||
s.add_development_dependency "simplecov" | ||
|
||
s.add_runtime_dependency "activerecord", '>= 3.2.0' | ||
spec.add_development_dependency "bundler", "~> 1.14" | ||
spec.add_development_dependency "rake", "~> 12.0" | ||
spec.add_development_dependency "rspec", "~> 3.5" | ||
spec.add_development_dependency "rubocop", "~> 0.47" | ||
end |
Oops, something went wrong.