pg_gnostic 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -7
- data/Rakefile +4 -2
- data/lib/pg_gnostic/railtie.rb +13 -0
- data/lib/pg_gnostic/view_definition.rb +2 -1
- data/lib/pg_gnostic.rb +3 -1
- data/rails/generators/pg_view/USAGE +5 -0
- data/rails/generators/pg_view/pg_view_generator.rb +34 -0
- data/{generators → rails/generators}/pg_view/templates/model.rb +0 -0
- data/{generators → rails/generators}/pg_view/templates/view.rb +0 -0
- data/{generators → rails/generators}/pg_view/templates/view.sql +0 -0
- data/test/config_test.rb +1 -1
- data/test/db/schema.rb +8 -8
- data/test/db/views/users.rb +2 -2
- data/test/pg_gnostic_test.rb +1 -1
- data/test/test_helper.rb +8 -13
- data/test/view_definition_test.rb +1 -3
- data/test/view_generator_test.rb +38 -33
- metadata +54 -14
- data/generators/pg_view/USAGE +0 -5
- data/generators/pg_view/pg_view_generator.rb +0 -50
- data/lib/pg_gnostic/tasks.rb +0 -1
data/README.rdoc
CHANGED
@@ -12,15 +12,10 @@ and then
|
|
12
12
|
|
13
13
|
sudo rake gems:install
|
14
14
|
|
15
|
-
To install pg_gnostic rake tasks u need add this lines into RAILS_ROOT/Rakefile
|
16
|
-
|
17
|
-
require 'pg_gnostic'
|
18
|
-
require 'pg_gnostic/tasks'
|
19
|
-
|
20
15
|
== FEATURES
|
21
16
|
|
22
17
|
* add pg views generator
|
23
|
-
|
18
|
+
rails generate pg_view [--format= ruby|sql] view_name [dependency_view_names]
|
24
19
|
* rake pg: pg:drop_views, pg:functions, pg:views and all in one pg:update
|
25
20
|
|
26
21
|
|
@@ -28,7 +23,9 @@ To install pg_gnostic rake tasks u need add this lines into RAILS_ROOT/Rakefile
|
|
28
23
|
|
29
24
|
0.0.2 - backup/restore rakes
|
30
25
|
0.0.3 - add PgGnostic::ViewDefinition#clear_declarations method
|
31
|
-
0.0.4 -
|
26
|
+
0.0.4 - kung_figure dependency updated
|
27
|
+
0.0.5 - compatibility with Ruby 1.9
|
28
|
+
0.1.0 - converted to Rails 3 format. No backward compatibility with Rails 2.x
|
32
29
|
|
33
30
|
== TODO
|
34
31
|
|
data/Rakefile
CHANGED
@@ -24,9 +24,9 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
24
24
|
end
|
25
25
|
|
26
26
|
|
27
|
-
PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'rails/**/*', 'tasks/**/*', 'test/**/*' ]
|
27
|
+
PKG_FILES = FileList[ '[a-zA-Z]*', 'rails/generators/**/*', 'lib/**/*', 'rails/**/*', 'tasks/**/*', 'test/**/*' ]
|
28
28
|
|
29
|
-
require 'lib/pg_gnostic'
|
29
|
+
require File.join(File.dirname(__FILE__), 'lib/pg_gnostic')
|
30
30
|
spec = Gem::Specification.new do |s|
|
31
31
|
s.name = "pg_gnostic"
|
32
32
|
s.version = PgGnostic::VERSION
|
@@ -40,6 +40,8 @@ spec = Gem::Specification.new do |s|
|
|
40
40
|
s.has_rdoc = false
|
41
41
|
s.extra_rdoc_files = ["README.rdoc"]
|
42
42
|
s.add_dependency('kung_figure')
|
43
|
+
s.add_dependency('rails', '>=3.0.0')
|
44
|
+
s.add_development_dependency('test-unit')
|
43
45
|
end
|
44
46
|
|
45
47
|
desc 'Turn this plugin into a gem.'
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module PgGnostic
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
generators do
|
6
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../rails/generators/pg_view/pg_view_generator'))
|
7
|
+
end
|
8
|
+
|
9
|
+
rake_tasks do
|
10
|
+
load File.expand_path(File.join(File.dirname(__FILE__),'../..','tasks','pg_gnostic_tasks.rake'))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/pg_gnostic.rb
CHANGED
@@ -3,12 +3,14 @@ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname
|
|
3
3
|
require 'kung_figure'
|
4
4
|
# PgGnostic
|
5
5
|
module PgGnostic
|
6
|
-
VERSION = "0.0
|
6
|
+
VERSION = "0.1.0"
|
7
7
|
include KungFigure
|
8
8
|
|
9
9
|
autoload :ViewDefinition, 'pg_gnostic/view_definition'
|
10
10
|
autoload :Config, 'pg_gnostic/config'
|
11
11
|
|
12
|
+
require 'pg_gnostic/railtie'
|
13
|
+
|
12
14
|
class << self
|
13
15
|
def define
|
14
16
|
yield ViewDefinition
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'pg_gnostic'
|
2
|
+
|
3
|
+
class PgViewGenerator < Rails::Generators::Base
|
4
|
+
desc "Generates pg view file"
|
5
|
+
class_option :format, :default => 'ruby', :aliases => "-f", :banner => 'sql | ruby'
|
6
|
+
argument :view_name, :required => true, :banner => "view name"
|
7
|
+
argument :dependencies, :type => :array, :required => false, :banner => "depends on views"
|
8
|
+
source_root File.dirname(__FILE__)
|
9
|
+
|
10
|
+
attr_reader :nest_in_module, :class_name, :dependencies, :prefix_view_name
|
11
|
+
|
12
|
+
def init_parameters
|
13
|
+
@view_name = self.view_name
|
14
|
+
@model_name = @view_name.singularize
|
15
|
+
@model_file_name = @model_name.underscore
|
16
|
+
@class_name = @model_name.classify
|
17
|
+
@dependencies = self.dependencies
|
18
|
+
@nest_in_module = PgGnostic.config.view_model.nest_in_module
|
19
|
+
@prefix_view_name = PgGnostic.config.view_model.prefix_view_name
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate
|
23
|
+
model_path = PgGnostic.config.view_model.model_path
|
24
|
+
empty_directory 'db/views'
|
25
|
+
empty_directory model_path
|
26
|
+
case options[:format]
|
27
|
+
when 'sql'
|
28
|
+
template "templates/view.sql", File.join('db/views', "#{@view_name}.sql")
|
29
|
+
when 'ruby'
|
30
|
+
template "templates/view.rb", File.join('db/views', "#{@view_name}.rb")
|
31
|
+
end
|
32
|
+
template "templates/model.rb", File.join(model_path, "#{@model_file_name}.rb")
|
33
|
+
end
|
34
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
data/test/config_test.rb
CHANGED
data/test/db/schema.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
ActiveRecord::Schema.define(:version => 0) do
|
2
2
|
create_table :users do |t|
|
3
|
-
t.string
|
4
|
-
t.string
|
5
|
-
t.string
|
6
|
-
t.string
|
7
|
-
t.string
|
8
|
-
t.string
|
9
|
-
t.string
|
10
|
-
t.string
|
3
|
+
t.string :last_name, :limit => 25
|
4
|
+
t.string :first_name, :limit => 25
|
5
|
+
t.string :middle_name, :limit => 25
|
6
|
+
t.string :name, :limit => 25
|
7
|
+
t.string :login, :limit => 40
|
8
|
+
t.string :email, :limit => 100
|
9
|
+
t.string :crypted_password, :limit => 40
|
10
|
+
t.string :salt, :limit => 40
|
11
11
|
t.datetime :last_login_datetime
|
12
12
|
t.datetime :deleted_at
|
13
13
|
t.timestamps
|
data/test/db/views/users.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PgGnostic.define do |d|
|
2
|
-
d.named_fields :my_exclude_fields,'crypted_password','salt','last_login_datetime','deleted_at'
|
3
2
|
d.create_view :view_users,:depends_on=>[:other_users], :sql=><<-SQL
|
4
3
|
SELECT
|
5
|
-
|
4
|
+
name, first_name, last_name
|
6
5
|
FROM users
|
7
6
|
SQL
|
8
7
|
end
|
8
|
+
# vim:ft=sql
|
data/test/pg_gnostic_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,31 +1,26 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'kung_figure'
|
3
|
+
|
3
4
|
def path(path)
|
4
|
-
File.join(File.dirname(__FILE__),path)
|
5
|
+
File.join(File.dirname(__FILE__), path).to_s
|
5
6
|
end
|
6
7
|
|
7
8
|
$:.unshift(path('../lib'))
|
9
|
+
require "test/unit"
|
8
10
|
require 'pg_gnostic'
|
9
11
|
require 'active_support'
|
10
12
|
require 'active_support/test_case'
|
11
|
-
require "rails_generator"
|
12
13
|
require "active_record"
|
13
|
-
require '
|
14
|
-
require "
|
14
|
+
require 'rails'
|
15
|
+
require "rails/generators"
|
15
16
|
|
17
|
+
#Test::Unit.run = true
|
16
18
|
|
17
19
|
GEM_ROOT= path('..')
|
18
|
-
Rails::Generator::Base.default_options :collision => :ask, :quiet => false
|
19
|
-
Rails::Generator::Base.reset_sources
|
20
|
-
Rails::Generator::Base.append_sources(Rails::Generator::PathSource.new(:plugin, "#{GEM_ROOT}/generators/"))
|
21
20
|
|
22
21
|
class GeneratorTest < ActiveSupport::TestCase
|
23
|
-
def generate(*args)
|
24
|
-
Rails::Generator::Scripts::Generate.new.run(args, :destination=>fake_rails_root)
|
25
|
-
end
|
26
|
-
|
27
22
|
def read(path)
|
28
|
-
IO.readlines("#{fake_rails_root}/#{path}",'').
|
23
|
+
[IO.readlines("#{fake_rails_root}/#{path}", '')].flatten.join
|
29
24
|
end
|
30
25
|
|
31
26
|
def assert_file(file)
|
@@ -92,4 +87,4 @@ class TestDbUtils
|
|
92
87
|
ActiveRecord::Base.connection.drop_database config[:database]
|
93
88
|
end
|
94
89
|
end
|
95
|
-
end
|
90
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.dirname(__FILE__)
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
2
|
#ActiveRecord::Base.logger=Logger.new(STDOUT)
|
3
3
|
|
4
4
|
TestDbUtils.ensure_schema
|
@@ -39,8 +39,6 @@ class PgGnosticViewUtilsTest < ActiveSupport::TestCase
|
|
39
39
|
assert_contain_field(OtherUser,'name')
|
40
40
|
assert_not_contain_field(OtherUser,'id')
|
41
41
|
assert_not_contain_field(OtherUser,'created_at')
|
42
|
-
assert_not_contain_field(OtherUser,'crypted_password')
|
43
|
-
assert_not_contain_field(OtherUser,'deleted_at')
|
44
42
|
end
|
45
43
|
|
46
44
|
def test_clear_definitions
|
data/test/view_generator_test.rb
CHANGED
@@ -1,43 +1,48 @@
|
|
1
|
-
require File.dirname(__FILE__)
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../rails/generators/pg_view/pg_view_generator"))
|
2
3
|
|
3
|
-
class PgGnosticViewsGeneratorTest < GeneratorTest
|
4
|
+
class PgGnosticViewsGeneratorTest < GeneratorTest
|
4
5
|
def test_generates_definition
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
6
|
+
FileUtils.chdir fake_rails_root do
|
7
|
+
PgGnostic.clear_config!
|
8
|
+
PgViewGenerator.new(['users', 'roles']).invoke_all
|
9
|
+
assert_file 'db/views/users.rb'
|
10
|
+
result = read 'db/views/users.rb'
|
11
|
+
assert_match(/view_users/, result)
|
12
|
+
assert_match(/view_roles/, result)
|
13
|
+
assert_match(/PgGnostic.define/, result)
|
14
|
+
assert_match(/depends_on/, result)
|
15
|
+
assert_file 'app/model/views/user.rb'
|
16
|
+
result = read 'app/model/views/user.rb'
|
17
|
+
assert_match(/class User < ActiveRecord::Base/, result)
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
PgViewGenerator.new(['roles'], {:format => 'sql'}).invoke_all
|
20
|
+
assert_file 'db/views/roles.sql'
|
21
|
+
result = read 'db/views/roles.sql'
|
22
|
+
assert_file 'app/model/views/role.rb'
|
23
|
+
result = read 'app/model/views/role.rb'
|
24
|
+
assert_match(/class Role < ActiveRecord::Base/, result)
|
25
|
+
end
|
23
26
|
end
|
24
27
|
|
25
28
|
def test_gen_with_changed_config
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
FileUtils.chdir fake_rails_root do
|
30
|
+
PgGnostic.clear_config!
|
31
|
+
PgGnostic.config.view_model.nest_in_module 'PgViews'
|
32
|
+
PgViewGenerator.new(['users', 'roles']).invoke_all
|
33
|
+
assert_file 'app/model/pg_views/user.rb'
|
34
|
+
result = read 'app/model/pg_views/user.rb'
|
35
|
+
assert_match(/module PgViews/, result)
|
32
36
|
|
33
|
-
|
34
|
-
|
35
|
-
|
37
|
+
PgGnostic.configure do
|
38
|
+
view_model do
|
39
|
+
nest_in_module ''
|
40
|
+
end
|
36
41
|
end
|
42
|
+
PgViewGenerator.new(['users', 'roles']).invoke_all
|
43
|
+
assert_file 'app/model/user.rb'
|
44
|
+
result = read 'app/model/user.rb'
|
45
|
+
assert_no_match(/module/, result)
|
37
46
|
end
|
38
|
-
generate 'pg_view','users','roles'
|
39
|
-
assert_file 'app/model/user.rb'
|
40
|
-
result = read 'app/model/user.rb'
|
41
|
-
assert_no_match(/module/, result)
|
42
47
|
end
|
43
|
-
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_gnostic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- niquola
|
@@ -9,19 +14,50 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-11-18 00:00:00 +03:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: kung_figure
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rails
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 3
|
43
|
+
- 0
|
44
|
+
- 0
|
45
|
+
version: 3.0.0
|
17
46
|
type: :runtime
|
18
|
-
|
19
|
-
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: test-unit
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
20
53
|
requirements:
|
21
54
|
- - ">="
|
22
55
|
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
23
58
|
version: "0"
|
24
|
-
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id003
|
25
61
|
description:
|
26
62
|
email: niquola@gmail.com
|
27
63
|
executables: []
|
@@ -34,13 +70,13 @@ files:
|
|
34
70
|
- README.rdoc
|
35
71
|
- Rakefile
|
36
72
|
- MIT-LICENSE
|
37
|
-
- generators/pg_view/USAGE
|
38
|
-
- generators/pg_view/pg_view_generator.rb
|
39
|
-
- generators/pg_view/templates/model.rb
|
40
|
-
- generators/pg_view/templates/view.sql
|
41
|
-
- generators/pg_view/templates/view.rb
|
73
|
+
- rails/generators/pg_view/USAGE
|
74
|
+
- rails/generators/pg_view/pg_view_generator.rb
|
75
|
+
- rails/generators/pg_view/templates/model.rb
|
76
|
+
- rails/generators/pg_view/templates/view.sql
|
77
|
+
- rails/generators/pg_view/templates/view.rb
|
78
|
+
- lib/pg_gnostic/railtie.rb
|
42
79
|
- lib/pg_gnostic/config.rb
|
43
|
-
- lib/pg_gnostic/tasks.rb
|
44
80
|
- lib/pg_gnostic/view_definition.rb
|
45
81
|
- lib/pg_gnostic.rb
|
46
82
|
- tasks/pg_gnostic_tasks.rake
|
@@ -67,21 +103,25 @@ rdoc_options: []
|
|
67
103
|
require_paths:
|
68
104
|
- lib
|
69
105
|
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
70
107
|
requirements:
|
71
108
|
- - ">="
|
72
109
|
- !ruby/object:Gem::Version
|
110
|
+
segments:
|
111
|
+
- 0
|
73
112
|
version: "0"
|
74
|
-
version:
|
75
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
76
115
|
requirements:
|
77
116
|
- - ">="
|
78
117
|
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
79
120
|
version: "0"
|
80
|
-
version:
|
81
121
|
requirements: []
|
82
122
|
|
83
123
|
rubyforge_project:
|
84
|
-
rubygems_version: 1.3.
|
124
|
+
rubygems_version: 1.3.7
|
85
125
|
signing_key:
|
86
126
|
specification_version: 3
|
87
127
|
summary: Rails plugin for postgres
|
data/generators/pg_view/USAGE
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
$:.unshift(path('../lib'))
|
2
|
-
require 'pg_gnostic'
|
3
|
-
class PgViewGenerator < Rails::Generator::Base
|
4
|
-
default_options :format => 'ruby'
|
5
|
-
def manifest
|
6
|
-
record do |m|
|
7
|
-
unless args.size > 0
|
8
|
-
puts "require view name"
|
9
|
-
exit 1
|
10
|
-
end
|
11
|
-
parse_args
|
12
|
-
model_path = PgGnostic.config.view_model.model_path
|
13
|
-
m.directory File.join('db/views')
|
14
|
-
m.directory File.join(model_path)
|
15
|
-
case options[:format]
|
16
|
-
when 'sql':
|
17
|
-
m.template "view.sql", File.join('db/views',"#{@view_name}.sql"), :assigns=>@assigns
|
18
|
-
when 'ruby':
|
19
|
-
m.template "view.rb", File.join('db/views',"#{@view_name}.rb"), :assigns=>@assigns
|
20
|
-
end
|
21
|
-
m.template "model.rb", File.join(model_path,"#{@model_file_name}.rb"), :assigns=>@assigns
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def parse_args
|
26
|
-
@view_name = args.shift.pluralize
|
27
|
-
@model_name = @view_name.singularize
|
28
|
-
@model_file_name = @model_name.underscore
|
29
|
-
@class_name = @model_name.classify
|
30
|
-
@dependencies = args if args.length > 0
|
31
|
-
@assigns = {
|
32
|
-
:view_name=>@view_name,
|
33
|
-
:class_name=>@class_name,
|
34
|
-
:dependencies=>@dependencies,
|
35
|
-
:nest_in_module=>PgGnostic.config.view_model.nest_in_module,
|
36
|
-
:prefix_view_name=>PgGnostic.config.view_model.prefix_view_name
|
37
|
-
}
|
38
|
-
end
|
39
|
-
|
40
|
-
protected
|
41
|
-
def banner
|
42
|
-
"Usage: #{$0} #{spec.name} ViewName"
|
43
|
-
end
|
44
|
-
|
45
|
-
def add_options!(opt)
|
46
|
-
opt.separator ''
|
47
|
-
opt.separator 'Options:'
|
48
|
-
opt.on("-f","--format=ruby", String, "sql | ruby","Default ruby") { |v| options[:format] = v }
|
49
|
-
end
|
50
|
-
end
|
data/lib/pg_gnostic/tasks.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
load File.join(File.dirname(__FILE__),'../..','tasks','pg_gnostic_tasks.rake')
|