mack-orm 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +3 -0
- data/doc/classes/Mack.html +131 -0
- data/doc/classes/Mack/Database.html +323 -0
- data/doc/classes/Mack/Database/Generators.html +140 -0
- data/doc/classes/Mack/Database/Migrations.html +250 -0
- data/doc/classes/Mack/ViewHelpers/OrmHelpers.html +105 -0
- data/doc/classes/ScaffoldGenerator.html +159 -0
- data/doc/created.rid +1 -0
- data/doc/files/README.html +111 -0
- data/doc/files/lib/mack-orm/database_migrations_rb.html +101 -0
- data/doc/files/lib/mack-orm/database_rb.html +101 -0
- data/doc/files/lib/mack-orm/generators_rb.html +101 -0
- data/doc/files/lib/mack-orm/genosaurus_helpers_rb.html +101 -0
- data/doc/files/lib/mack-orm/model_column_rb.html +101 -0
- data/doc/files/lib/mack-orm/orm_helpers_rb.html +101 -0
- data/doc/files/lib/mack-orm/scaffold_generator/scaffold_generator_rb.html +114 -0
- data/doc/files/lib/mack-orm_rb.html +109 -0
- data/doc/files/lib/mack-orm_tasks_rb.html +101 -0
- data/doc/fr_class_index.html +32 -0
- data/doc/fr_file_index.html +36 -0
- data/doc/fr_method_index.html +40 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +208 -0
- data/lib/mack-orm.rb +11 -0
- data/lib/mack-orm/database.rb +42 -0
- data/lib/mack-orm/database_migrations.rb +31 -0
- data/lib/mack-orm/generators.rb +11 -0
- data/lib/mack-orm/genosaurus_helpers.rb +40 -0
- data/lib/mack-orm/model_column.rb +50 -0
- data/lib/mack-orm/orm_helpers.rb +28 -0
- data/lib/mack-orm/scaffold_generator/manifest.yml +31 -0
- data/lib/mack-orm/scaffold_generator/scaffold_generator.rb +48 -0
- data/lib/mack-orm/scaffold_generator/templates/app/controllers/controller.rb.template +51 -0
- data/lib/mack-orm/scaffold_generator/templates/app/views/edit.html.erb.template +18 -0
- data/lib/mack-orm/scaffold_generator/templates/app/views/index.html.erb.template +41 -0
- data/lib/mack-orm/scaffold_generator/templates/app/views/new.html.erb.template +18 -0
- data/lib/mack-orm/scaffold_generator/templates/app/views/show.html.erb.template +12 -0
- data/lib/mack-orm/scaffold_generator/templates/test/controllers/rspec.rb.template +47 -0
- data/lib/mack-orm/scaffold_generator/templates/test/controllers/test_case.rb.template +9 -0
- data/lib/mack-orm/tasks/db_create_drop_tasks.rake +72 -0
- data/lib/mack-orm/tasks/db_migration_tasks.rake +18 -0
- data/lib/mack-orm/tasks/test_tasks.rake +29 -0
- data/lib/mack-orm_tasks.rb +3 -0
- metadata +96 -0
data/lib/mack-orm.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'genosaurus'
|
3
|
+
|
4
|
+
base = File.join(File.dirname(__FILE__), "mack-orm")
|
5
|
+
require File.join(base, "database")
|
6
|
+
require File.join(base, "database_migrations")
|
7
|
+
require File.join(base, "generators")
|
8
|
+
require File.join(base, "genosaurus_helpers")
|
9
|
+
require File.join(base, "model_column")
|
10
|
+
require File.join(base, "orm_helpers")
|
11
|
+
require File.join(base, "scaffold_generator", "scaffold_generator")
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Mack
|
2
|
+
module Database
|
3
|
+
|
4
|
+
# Sets up and establishes connections to the database based on the specified environment
|
5
|
+
# and the settings in the database.yml file.
|
6
|
+
def self.establish_connection(env = Mack.env)
|
7
|
+
raise NoMethodError.new(:establish_connection)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Clears connections to the database
|
11
|
+
def self.clear_connection(env = Mack.env)
|
12
|
+
raise NoMethodError.new(:clear_connection)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Creates a database, if it doesn't already exist for the specified environment
|
16
|
+
def self.create(env = Mack.env, repis = :default)
|
17
|
+
raise NoMethodError.new(:create)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Drops a database, if it exists for the specified environment
|
21
|
+
def self.drop(env = Mack.env, repis = :default)
|
22
|
+
raise NoMethodError.new(:drop)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Drops and then creates the database.
|
26
|
+
def self.recreate(env = Mack.env, repis = :default)
|
27
|
+
Mack::Database.drop(env, repis)
|
28
|
+
Mack::Database.create(env, repis)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Loads the structure of the given file into the database
|
32
|
+
def self.load_structure(file, env = Mack.env, repis = :default)
|
33
|
+
raise NoMethodError.new(:load_structure)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Dumps the structure of the database to a file.
|
37
|
+
def self.dump_structure(env = Mack.env, repis = :default)
|
38
|
+
raise NoMethodError.new(:dump_structure)
|
39
|
+
end
|
40
|
+
|
41
|
+
end # Database
|
42
|
+
end # Mack
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Mack
|
2
|
+
module Database
|
3
|
+
module Migrations
|
4
|
+
|
5
|
+
# Migrates the database to the latest version
|
6
|
+
def self.migrate
|
7
|
+
raise NoMethodError.new(:migrate)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Rolls back the database by the specified number of steps. Default is 1
|
11
|
+
def self.rollback(step = 1)
|
12
|
+
raise NoMethodError.new(:rollback)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.abort_if_pending_migrations
|
16
|
+
raise NoMethodError.new(:abort_if_pending_migrations)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns the current version of the database
|
20
|
+
def self.version
|
21
|
+
raise NoMethodError.new(:version)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns a list of the all migration files.
|
25
|
+
def self.migration_files
|
26
|
+
Dir.glob(File.join(Mack.root, "db", "migrations", "*.rb"))
|
27
|
+
end
|
28
|
+
|
29
|
+
end # Migrations
|
30
|
+
end # Database
|
31
|
+
end # Mack
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Mack
|
2
|
+
module Genosaurus # :nodoc:
|
3
|
+
module Orm # :nodoc:
|
4
|
+
module Helpers # :nodoc:
|
5
|
+
|
6
|
+
def columns(name = param(:name))
|
7
|
+
ivar_cache("form_columns") do
|
8
|
+
cs = []
|
9
|
+
cols = (param(:cols) || param(:columns))
|
10
|
+
if cols
|
11
|
+
cols.split(",").each do |x|
|
12
|
+
cs << Mack::Genosaurus::Orm::ModelColumn.new(name, x)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
cs
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def db_directory
|
20
|
+
File.join(Mack.root, "db")
|
21
|
+
end
|
22
|
+
|
23
|
+
def migrations_directory
|
24
|
+
File.join(db_directory, "migrations")
|
25
|
+
end
|
26
|
+
|
27
|
+
def next_migration_number
|
28
|
+
last = Dir.glob(File.join(migrations_directory, "*.rb")).last
|
29
|
+
if last
|
30
|
+
return File.basename(last).match(/^\d+/).to_s.succ
|
31
|
+
end
|
32
|
+
return "001"
|
33
|
+
end
|
34
|
+
|
35
|
+
::Genosaurus.send(:include, self)
|
36
|
+
|
37
|
+
end # Helpers
|
38
|
+
end # Orm
|
39
|
+
end # Genosaurus
|
40
|
+
end # Mack
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Mack
|
2
|
+
module Genosaurus # :nodoc:
|
3
|
+
module Orm # :nodoc:
|
4
|
+
# Used to represent a 'column' from the param cols or columns for generators.
|
5
|
+
class ModelColumn # :nodoc:
|
6
|
+
|
7
|
+
# The name of the column.
|
8
|
+
attr_accessor :column_name
|
9
|
+
# The type of the column. Ie. string, integer, datetime, etc...
|
10
|
+
attr_accessor :column_type
|
11
|
+
# The name of the model associated with the column. Ie. user, post, etc...
|
12
|
+
attr_accessor :model_name
|
13
|
+
|
14
|
+
# Takes in the model_name (user, post, etc...) and the column (username:string, body:text, etc...)
|
15
|
+
def initialize(model_name, column_unsplit)
|
16
|
+
self.model_name = model_name.singular.underscore
|
17
|
+
cols = column_unsplit.split(":")
|
18
|
+
self.column_name = cols.first#.underscore
|
19
|
+
self.column_type = cols.last#.underscore
|
20
|
+
end
|
21
|
+
|
22
|
+
# Generates the appropriate HTML form field for the type of column represented.
|
23
|
+
#
|
24
|
+
# Examples:
|
25
|
+
# Mack::Generator::ColumnObject.new("user", "username:string").form_field
|
26
|
+
# => "<%= :user.text_field :username, :label => true %>"
|
27
|
+
# Mack::Generator::ColumnObject.new("Post", "body:text").form_field
|
28
|
+
# => "<%= :post.text_area :body, :label => true %>"
|
29
|
+
def form_field
|
30
|
+
case self.column_type
|
31
|
+
when "text"
|
32
|
+
%{<%= :#{self.model_name}.text_area :#{self.column_name}, :label => true %>}
|
33
|
+
when "date"
|
34
|
+
%{<%= :#{self.model_name}.date_select :#{self.column_name}, :label => true %>}
|
35
|
+
when "date_time"
|
36
|
+
%{<%= :#{self.model_name}.date_time_select :#{self.column_name}, :label => true %>}
|
37
|
+
else
|
38
|
+
case self.column_name.downcase
|
39
|
+
when /password/
|
40
|
+
%{<%= :#{self.model_name}.password_field :#{self.column_name}, :label => true %>}
|
41
|
+
else
|
42
|
+
%{<%= :#{self.model_name}.text_field :#{self.column_name}, :label => true %>}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end # ModelColumn
|
48
|
+
end # Orm
|
49
|
+
end # Generator
|
50
|
+
end # Mack
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mack
|
2
|
+
module ViewHelpers # :nodoc:
|
3
|
+
module OrmHelpers
|
4
|
+
|
5
|
+
# DEPRECATED: Use Mack::ViewHelpers::FormHelpers text_field instead.
|
6
|
+
def model_text_field(model, property, options = {}) # :nodoc:
|
7
|
+
deprecate_method(:model_text_field, :text_field, '0.7.0')
|
8
|
+
m_name = model.class.to_s.underscore
|
9
|
+
text_field(model.class.to_s.underscore, property, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
# DEPRECATED: Use Mack::ViewHelpers::FormHelpers password_field instead.
|
13
|
+
def model_password_field(model, property, options = {}) # :nodoc:
|
14
|
+
deprecate_method(:model_password_field, :password_field, '0.7.0')
|
15
|
+
m_name = model.class.to_s.underscore
|
16
|
+
password_field(model.class.to_s.underscore, property, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
# DEPRECATED: Use Mack::ViewHelpers::FormHelpers text_area instead.
|
20
|
+
def model_textarea(model, property, options = {}) # :nodoc:
|
21
|
+
deprecate_method(:model_textarea, :text_area, '0.7.0')
|
22
|
+
m_name = model.class.to_s.underscore
|
23
|
+
text_area(model.class.to_s.underscore, property, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
end # OrmHelpers
|
27
|
+
end # ViewHelpers
|
28
|
+
end # Mack
|
@@ -0,0 +1,31 @@
|
|
1
|
+
controller_template:
|
2
|
+
type: file
|
3
|
+
template_path: <%= Mack::Database::Generators.controller_template_location %>
|
4
|
+
output_path: <%= File.join("app", "controllers", "#{@name_plural}_controller.rb") %>
|
5
|
+
edit_template:
|
6
|
+
type: file
|
7
|
+
template_path: <%= File.join(templates_directory_path, "app", "views", "edit.html.erb.template") %>
|
8
|
+
output_path: <%= File.join("app", "views", @name_plural, "edit.html.erb") %>
|
9
|
+
index_template:
|
10
|
+
type: file
|
11
|
+
template_path: <%= File.join(templates_directory_path, "app", "views", "index.html.erb.template") %>
|
12
|
+
output_path: <%= File.join("app", "views", @name_plural, "index.html.erb") %>
|
13
|
+
new_template:
|
14
|
+
type: file
|
15
|
+
template_path: <%= File.join(templates_directory_path, "app", "views", "new.html.erb.template") %>
|
16
|
+
output_path: <%= File.join("app", "views", @name_plural, "new.html.erb") %>
|
17
|
+
show_template:
|
18
|
+
type: file
|
19
|
+
template_path: <%= File.join(templates_directory_path, "app", "views", "show.html.erb.template") %>
|
20
|
+
output_path: <%= File.join("app", "views", @name_plural, "show.html.erb") %>
|
21
|
+
<% if app_config.mack.testing_framework == "test_case" -%>
|
22
|
+
functional_test_template:
|
23
|
+
type: file
|
24
|
+
template_path: <%= File.join(templates_directory_path, "test", "controllers", "test_case.rb.template") %>
|
25
|
+
output_path: <%= File.join("test", "controllers", "#{@name_plural}_controller_test.rb") %>
|
26
|
+
<% elsif app_config.mack.testing_framework == "rspec" -%>
|
27
|
+
functional_test_template:
|
28
|
+
type: file
|
29
|
+
template_path: <%= File.join(templates_directory_path, "test", "controllers", "rspec.rb.template") %>
|
30
|
+
output_path: <%= File.join("test", "controllers", "#{@name_plural}_controller_spec.rb") %>
|
31
|
+
<% end -%>
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Generates scaffold for Mack applications.
|
2
|
+
#
|
3
|
+
# Example:
|
4
|
+
# rake generate:scaffold name=post
|
5
|
+
class ScaffoldGenerator < Genosaurus
|
6
|
+
|
7
|
+
require_param :name
|
8
|
+
|
9
|
+
def setup # :nodoc:
|
10
|
+
@name_singular = param(:name).singular.underscore
|
11
|
+
@name_plural = param(:name).plural.underscore
|
12
|
+
@name_singular_camel = @name_singular.camelcase
|
13
|
+
@name_plural_camel = @name_plural.camelcase
|
14
|
+
@test_framework = app_config.mack.testing_framework
|
15
|
+
end
|
16
|
+
|
17
|
+
def after_generate # :nodoc:
|
18
|
+
ModelGenerator.run(@options)
|
19
|
+
ControllerHelperGenerator.run(@options)
|
20
|
+
update_routes_file
|
21
|
+
end
|
22
|
+
|
23
|
+
def showable_columns
|
24
|
+
cols = columns.reject {|c| c.column_name.downcase.match(/password/)}
|
25
|
+
cols
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_routes_file # :nodoc:
|
29
|
+
# update routes.rb
|
30
|
+
routes = File.join(Mack.root, "config", "routes.rb")
|
31
|
+
rf = File.open(routes).read
|
32
|
+
unless rf.match(".resource :#{@name_plural}")
|
33
|
+
puts "Updating routes.rb"
|
34
|
+
nrf = ""
|
35
|
+
rf.each do |line|
|
36
|
+
if line.match("Mack::Routes.build")
|
37
|
+
x = line.match(/\|(.+)\|/).captures
|
38
|
+
line << "\n #{x}.resource :#{@name_plural} # Added by rake generate:scaffold name=#{param(:name)}\n"
|
39
|
+
end
|
40
|
+
nrf << line
|
41
|
+
end
|
42
|
+
File.open(routes, "w") do |f|
|
43
|
+
f.puts nrf
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class <%= @name_plural_camel %>Controller
|
2
|
+
include Mack::Controller
|
3
|
+
|
4
|
+
# GET /<%= @name_plural %>
|
5
|
+
def index
|
6
|
+
@<%= @name_plural %> = <%= @name_singular_camel %>.all
|
7
|
+
end
|
8
|
+
|
9
|
+
# GET /<%= @name_plural %>/1
|
10
|
+
def show
|
11
|
+
@<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
|
12
|
+
end
|
13
|
+
|
14
|
+
# GET /<%= @name_plural %>/new
|
15
|
+
def new
|
16
|
+
@<%= @name_singular %> = <%= @name_singular_camel %>.new
|
17
|
+
end
|
18
|
+
|
19
|
+
# GET /<%= @name_plural %>/1/edit
|
20
|
+
def edit
|
21
|
+
@<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
|
22
|
+
end
|
23
|
+
|
24
|
+
# POST /<%= @name_plural %>
|
25
|
+
def create
|
26
|
+
@<%= @name_singular %> = <%= @name_singular_camel %>.new(params[:<%= @name_singular %>])
|
27
|
+
if @<%= @name_singular %>.save
|
28
|
+
redirect_to(<%= @name_plural %>_show_url(:id => @<%= @name_singular %>))
|
29
|
+
else
|
30
|
+
render(:action, "new")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# PUT /<%= @name_plural %>/1
|
35
|
+
def update
|
36
|
+
@<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
|
37
|
+
if @<%= @name_singular %>.update_attributes(params[:<%= @name_singular %>])
|
38
|
+
redirect_to(<%= @name_plural %>_show_url(:id => @<%= @name_singular %>))
|
39
|
+
else
|
40
|
+
render(:action, "edit")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# DELETE /<%= @name_plural %>/1
|
45
|
+
def delete
|
46
|
+
@<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
|
47
|
+
@<%= @name_singular %>.destroy
|
48
|
+
redirect_to(<%= @name_plural %>_index_url)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<h1>Edit <%= @name_singular.humanize.titlecase %></h1>
|
2
|
+
|
3
|
+
<%%= error_messages_for :<%= @name_singular %> %>
|
4
|
+
|
5
|
+
<%% form(<%= @name_singular %>s_update_url(:id => @<%= @name_singular %>), :class => "edit_<%= @name_singular %>", :id => "edit_<%= @name_singular %>", :method => :put) do %>
|
6
|
+
<% for column in columns -%>
|
7
|
+
<% unless column.column_name == "created_at" || column.column_name == "updated_at" -%>
|
8
|
+
<p>
|
9
|
+
<%= column.form_field %>
|
10
|
+
</p>
|
11
|
+
<% end -%>
|
12
|
+
<% end -%>
|
13
|
+
<p>
|
14
|
+
<%%= submit_button("Update") %>
|
15
|
+
</p>
|
16
|
+
<%% end %>
|
17
|
+
|
18
|
+
<%%= link_to("Back", <%= @name_singular %>s_index_url) %>
|
@@ -0,0 +1,41 @@
|
|
1
|
+
<h1>Listing <%= @name_plural.humanize.titlecase %></h1>
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<tr>
|
5
|
+
<%
|
6
|
+
unless showable_columns.empty?
|
7
|
+
showable_columns.each do |col|
|
8
|
+
-%>
|
9
|
+
<th><%= col.column_name.humanize %></th>
|
10
|
+
<%
|
11
|
+
end
|
12
|
+
else
|
13
|
+
-%>
|
14
|
+
<th> </th>
|
15
|
+
<%
|
16
|
+
end
|
17
|
+
-%>
|
18
|
+
</tr>
|
19
|
+
|
20
|
+
<%% for <%= @name_singular %> in @<%= @name_plural %> %>
|
21
|
+
<tr>
|
22
|
+
<%
|
23
|
+
unless showable_columns.empty?
|
24
|
+
showable_columns.each do |col| -%>
|
25
|
+
<td><%%= <%= @name_singular %>.<%= col.column_name %> %></td>
|
26
|
+
<%
|
27
|
+
end
|
28
|
+
else
|
29
|
+
-%>
|
30
|
+
<td> </td>
|
31
|
+
<% end -%>
|
32
|
+
<td><%%= link_to("Show", <%= @name_plural %>_show_url(:id => <%= @name_singular %>)) %></td>
|
33
|
+
<td><%%= link_to("Edit", <%= @name_plural %>_edit_url(:id => <%= @name_singular %>)) %></td>
|
34
|
+
<td><%%= link_to("Delete", <%= @name_plural %>_delete_url(:id => <%= @name_singular %>), :method => :delete, :confirm => "Are you sure?") %></td>
|
35
|
+
</tr>
|
36
|
+
<%% end %>
|
37
|
+
</table>
|
38
|
+
|
39
|
+
<br />
|
40
|
+
|
41
|
+
<%%= link_to("New <%= @name_singular_camel %>", <%= @name_plural %>_new_url) %>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<h1>New <%= @name_singular.humanize.titlecase %></h1>
|
2
|
+
|
3
|
+
<%%= error_messages_for :<%= @name_singular %> %>
|
4
|
+
|
5
|
+
<%% form(<%= @name_singular %>s_create_url, :class => "new_<%= @name_singular %>", :id => "new_<%= @name_singular %>") do %>
|
6
|
+
<% for column in columns -%>
|
7
|
+
<% unless column.column_name == "created_at" || column.column_name == "updated_at" -%>
|
8
|
+
<p>
|
9
|
+
<%= column.form_field %>
|
10
|
+
</p>
|
11
|
+
<% end -%>
|
12
|
+
<% end -%>
|
13
|
+
<p>
|
14
|
+
<%%= submit_button("Create") %>
|
15
|
+
</p>
|
16
|
+
<%% end %>
|
17
|
+
|
18
|
+
<%%= link_to("Back", <%= @name_singular %>s_index_url) %>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<p>
|
2
|
+
<h1><%= @name_singular.humanize.titlecase %></h1>
|
3
|
+
</p>
|
4
|
+
<% for column in showable_columns -%>
|
5
|
+
<p>
|
6
|
+
<%%= :<%= @name_singular %>.label_tag :<%= column.column_name %> %>
|
7
|
+
<%%= @<%= @name_singular %>.<%= column.column_name %> %>
|
8
|
+
</p>
|
9
|
+
<% end -%>
|
10
|
+
|
11
|
+
<%%= link_to("Edit", <%= @name_plural %>_edit_url(:id => @<%= @name_singular %>)) %> |
|
12
|
+
<%%= link_to("Back", <%= @name_plural %>_index_url) %>
|