ARAA 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +43 -0
- data/Rakefile +27 -0
- data/lib/araa.rb +7 -0
- data/lib/araa/active_record.rb +25 -0
- data/lib/araa/railtie.rb +7 -0
- data/lib/araa/table.rb +78 -0
- data/lib/araa/version.rb +3 -0
- data/lib/tasks/araa_tasks.rake +39 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 63eba5d272d3d2ea69cdda4d6f4c816e9efe5e4a36b1f7731e41f320601dd826
|
4
|
+
data.tar.gz: b627cdc8413064a47de774e389e5bb9e4f47e0446c005167cedb7b3785a56b2d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a049f02bbd4d558d4f4c60e68625c98e28be9f7f8db6c24412b2eb66de06a194055c23f4978d0fae5cdf40a6aa98269a8b8954a564c67250c82959bdc28c5699
|
7
|
+
data.tar.gz: '091db9c3597d16a9008715c5e7e5aa5f04f95d184ae974739f41ed8e8c46f301b0eb6363b509ce5859cf913958ec0c388d9f770a167844ed35465bf23965618d'
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2020
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# App
|
2
|
+
ARAA (ActiveRecord Auto Associate) is a Gem that allows you to dynamically build
|
3
|
+
your models associations without having to explicitly define them.
|
4
|
+
|
5
|
+
It does this by inspecting the defined schema via ActiveRecord and populates
|
6
|
+
an initializer file. This file contains class declarations of your models that
|
7
|
+
use re-open them and defines ActiveRecord relations.
|
8
|
+
|
9
|
+
This is generated via a Rake task.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
`rake araa:generate`
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'araa'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
```bash
|
23
|
+
$ bundle
|
24
|
+
```
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
```bash
|
28
|
+
$ gem install araa
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
Please feel free to contribute.
|
33
|
+
|
34
|
+
At the moment, I don't know how to test this... If anyone has experience
|
35
|
+
testing the effects of a plugins effect on the dummy application I would
|
36
|
+
love the insight.
|
37
|
+
|
38
|
+
## License
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Future
|
42
|
+
|
43
|
+
- [ ] Tie into Migration event and rerun
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Araa'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
data/lib/araa.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'araa/table'
|
2
|
+
|
3
|
+
module Araa
|
4
|
+
class ActiveRecord
|
5
|
+
def self.filtered_application_tables
|
6
|
+
# Removes the rails meta tables. Only returns application defined tables.
|
7
|
+
::ApplicationRecord.connection.tables.reject do |t|
|
8
|
+
Araa::RAILS_META_TABLES.include?(t)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.tables_field_map
|
13
|
+
table_fields = []
|
14
|
+
|
15
|
+
filtered_application_tables.each do |t|
|
16
|
+
table = Araa::Table.new(t)
|
17
|
+
table.fields.each do |field|
|
18
|
+
table_fields << "#{t}::#{field}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
table_fields
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/araa/railtie.rb
ADDED
data/lib/araa/table.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'araa/active_record'
|
2
|
+
|
3
|
+
module Araa
|
4
|
+
class Table
|
5
|
+
attr_accessor :belongs_to,
|
6
|
+
:has_many,
|
7
|
+
:has_many_through
|
8
|
+
|
9
|
+
def initialize(name, build=false)
|
10
|
+
@name = name
|
11
|
+
@belongs_to = []
|
12
|
+
@has_one = []
|
13
|
+
@has_one_through = []
|
14
|
+
@has_many = []
|
15
|
+
@has_many_through = []
|
16
|
+
@has_and_belongs_to_many = []
|
17
|
+
|
18
|
+
if build
|
19
|
+
find_belongs_to
|
20
|
+
find_has_many
|
21
|
+
find_has_many_through
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def table_name
|
26
|
+
@name
|
27
|
+
end
|
28
|
+
|
29
|
+
def model_name
|
30
|
+
table_name.singularize.camelize
|
31
|
+
end
|
32
|
+
|
33
|
+
def model
|
34
|
+
model_name.constantize
|
35
|
+
end
|
36
|
+
|
37
|
+
def fields
|
38
|
+
model.column_names.reject { |c| Araa::RAILS_META_COLUMNS.include?(c) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def has_relations?
|
42
|
+
@belongs_to.any? || @has_many.any? || @has_many_through.any?
|
43
|
+
end
|
44
|
+
|
45
|
+
def is_habtm?
|
46
|
+
# a table is considered a has_and_belongs_to_many table if it only has
|
47
|
+
# foreign key columns.
|
48
|
+
fields.all? { |field| field.match(/_id$/) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def find_belongs_to
|
52
|
+
fields.each { |field|
|
53
|
+
next unless field.match(/_id$/)
|
54
|
+
@belongs_to << field.gsub('_id', '')
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def find_has_many(taf=Araa::ActiveRecord.tables_field_map)
|
59
|
+
taf.each do |table_field|
|
60
|
+
if Regexp.new("#{table_name.singularize}_id").match(table_field)
|
61
|
+
@has_many << table_field.split('::').first
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def find_has_many_through(taf=Araa::ActiveRecord.tables_field_map)
|
67
|
+
@has_many.each do |hm|
|
68
|
+
associated_table = Araa::Table.new(hm)
|
69
|
+
associated_table.find_belongs_to
|
70
|
+
|
71
|
+
associated_table.belongs_to.each do |bt|
|
72
|
+
next if bt == table_name.singularize
|
73
|
+
@has_many_through << [bt.pluralize, hm]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/araa/version.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'araa/active_record'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
namespace :araa do
|
5
|
+
desc 'Creates the Araa config file if it does not exist.'
|
6
|
+
task create_models_config: :environment do
|
7
|
+
path = File.join(Rails.root, 'config', 'initializers', 'araa_models.rb')
|
8
|
+
FileUtils.touch path
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Generates the associations and stores them in an initializer"
|
12
|
+
task generate: [:environment, :create_models_config] do
|
13
|
+
path = File.join(Rails.root, 'config', 'initializers', 'araa_models.rb')
|
14
|
+
tables_and_fields = Araa::ActiveRecord.tables_field_map
|
15
|
+
|
16
|
+
tables = Araa::ActiveRecord.filtered_application_tables.map do |t|
|
17
|
+
table = Araa::Table.new(t)
|
18
|
+
table.find_belongs_to
|
19
|
+
table.find_has_many(tables_and_fields)
|
20
|
+
table.find_has_many_through(tables_and_fields)
|
21
|
+
table
|
22
|
+
end
|
23
|
+
|
24
|
+
File.open(path, 'w') do |f|
|
25
|
+
tables.each do |table|
|
26
|
+
next unless table.has_relations?
|
27
|
+
f.write "class #{table.model_name} < ApplicationRecord\n"
|
28
|
+
|
29
|
+
table.belongs_to.each { |bt| f.write "\tbelongs_to :#{bt}\n" }
|
30
|
+
table.has_many.each { |bt| f.write "\thas_many :#{bt}\n" }
|
31
|
+
table.has_many_through.each { |hmt|
|
32
|
+
f.write "\thas_many :#{hmt[0]}, through: :#{hmt[1]}\n"
|
33
|
+
}
|
34
|
+
|
35
|
+
f.write "end\n\n"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ARAA
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Turknett
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.0.2
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 6.0.2.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 6.0.2
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 6.0.2.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: sqlite3
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
description: Automatically build ActiveRecord relations for models based on Rails
|
48
|
+
conventions
|
49
|
+
email:
|
50
|
+
- johnturknett@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- MIT-LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/araa.rb
|
59
|
+
- lib/araa/active_record.rb
|
60
|
+
- lib/araa/railtie.rb
|
61
|
+
- lib/araa/table.rb
|
62
|
+
- lib/araa/version.rb
|
63
|
+
- lib/tasks/araa_tasks.rake
|
64
|
+
homepage: https://github.com/johnTurknett/araa
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.0.3
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: ActiveRecord Auto Associate
|
87
|
+
test_files: []
|