mack-orm 0.8.0.101 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mack-orm.rb CHANGED
@@ -3,6 +3,7 @@ require 'genosaurus'
3
3
  require 'configatron'
4
4
 
5
5
  configatron.mack.set_default(:disable_transactional_tests, false)
6
+ configatron.mack.database.pagination.set_default(:results_per_page, 10)
6
7
 
7
8
  base = File.join(File.dirname(__FILE__), "mack-orm")
8
9
  require File.join(base, "database")
@@ -11,8 +12,54 @@ require File.join(base, "generators")
11
12
  require File.join(base, "genosaurus_helpers")
12
13
  require File.join(base, "model_column")
13
14
  require File.join(base, "test_extensions")
15
+ require File.join(base, "paginator")
14
16
  require File.join(base, "scaffold_generator", "scaffold_generator")
15
17
 
16
18
  Mack::Environment.after_class_method(:load) do
17
19
  Mack::Database.establish_connection(Mack.env)
18
- end
20
+ end
21
+
22
+ Mack::Portlet::Unpacker.registered_items[:migrations] = Proc.new do |force|
23
+ local_files = Dir.glob(Mack::Paths.migrations('**/*')).sort
24
+ num = '001'
25
+ unless local_files.empty?
26
+ num = File.basename(local_files.last).match(/(\d+)_.+/).captures.first
27
+ end
28
+ files = []
29
+ Mack.search_path(:db, false).each do |path|
30
+ files << Dir.glob(File.join(path, 'migrations', '**/*'))
31
+ end
32
+ files = files.flatten.compact.uniq.sort
33
+ files.each do |f|
34
+ fname = File.basename(f)
35
+ base = fname.match(/\d+_(.+)/).captures.first
36
+ if local_files.include?(/\d+_#{base}/) && !force
37
+ puts "Skipping: #{base}"
38
+ elsif local_files.include?(/\d+_#{base}/) && force
39
+ local_files.each do |lf|
40
+ if lf.match(/\d+_#{base}/)
41
+ n = lf.match(/(\d+)_#{base}/).captures.first
42
+ puts "Overwriting: #{base}"
43
+ src = File.read(f)
44
+ src.scan(/migration\D+\d+/).each do |line|
45
+ src.gsub!(line, line.gsub(/\d+/, n))
46
+ end
47
+ File.open(lf, 'w') do |w|
48
+ w.puts src
49
+ end
50
+ break
51
+ end
52
+ end
53
+ else
54
+ puts "Creating: #{"#{num}_#{base}"}"
55
+ num = num.succ
56
+ src = File.read(f)
57
+ src.scan(/migration\D+\d+/).each do |line|
58
+ src.gsub!(line, line.gsub(/\d+/, num))
59
+ end
60
+ File.open(Mack::Paths.migrations("#{num}_#{base}"), 'w') do |w|
61
+ w.puts src
62
+ end # File.open
63
+ end # if
64
+ end # files.each
65
+ end # Proc
@@ -23,7 +23,16 @@ module Mack
23
23
 
24
24
  # Returns a list of the all migration files.
25
25
  def self.migration_files
26
- Dir.glob(Mack::Paths.migrations("*.rb"))
26
+ allmigs = {}
27
+ Mack.search_path_local_first(:db).each do |path|
28
+ Dir.glob(File.join(path, 'migrations', '*.rb')).each do |f|
29
+ base = File.basename(f).match(/\d+_(.+)/).captures.first.downcase
30
+ unless allmigs[base]
31
+ allmigs[base] = f
32
+ end
33
+ end
34
+ end
35
+ allmigs.collect {|k,v| v}.sort
27
36
  end
28
37
 
29
38
  end # Migrations
@@ -0,0 +1,81 @@
1
+ module Mack
2
+ module Database
3
+
4
+ # Creates a new Mack::Database::Paginator class and calls the paginate method on it.
5
+ def self.paginate(klass, options = {}, query_options = {})
6
+ paginator = Mack::Database::Paginator.new(klass, options, query_options)
7
+ paginator.paginate
8
+ end
9
+
10
+ # This class provides a clean API for doing database pagination.
11
+ # The paginate methods needs to be implemented by the ORM developer.
12
+ class Paginator
13
+
14
+ # The Class that all queries will be called on
15
+ attr_accessor :klass
16
+ # Options for the Paginator itself
17
+ attr_accessor :options
18
+ # Options for the queries to be run
19
+ attr_accessor :query_options
20
+ # The total rows returned by the Paginator
21
+ attr_accessor :total_results
22
+ # The total pages available
23
+ attr_accessor :total_pages
24
+ # The actual records themselves
25
+ attr_accessor :results
26
+ # The current page in the pagination. Default is 1.
27
+ attr_accessor :current_page
28
+ # The number of results to be returned per page.
29
+ attr_accessor :results_per_page
30
+
31
+ # Takes the Class that all queries will be run on, options for the Paginator,
32
+ # and options for the queries that are going to be run.
33
+ def initialize(klass, options = {}, query_options = {})
34
+ self.klass = klass
35
+ self.options = options
36
+ self.query_options = query_options
37
+ self.current_page = (self.options.delete(:current_page) || 1).to_i
38
+ self.results_per_page = (self.options.delete(:results_per_page) || configatron.mack.database.pagination.results_per_page).to_i
39
+ end
40
+
41
+ # Implement this method in your ORM package. It should return <tt>self</tt> and set the following
42
+ # accessors: <tt>total_results</tt>, <tt>total_pages</tt>, <tt>results</tt>.
43
+ def paginate
44
+ raise NoMethodError.new('paginate')
45
+ end
46
+
47
+ # Is there a next page?
48
+ def has_next?
49
+ return self.current_page != self.total_pages && self.total_pages > 1
50
+ end
51
+
52
+ # Is there a previous page?
53
+ def has_previous?
54
+ return self.current_page != 1 && self.total_pages > 1
55
+ end
56
+
57
+ # The starting index for this group of results.
58
+ # Useful for building things like:
59
+ # Displaying 11 - 20 of 56 results.
60
+ def start_index
61
+ return 0 if self.total_results == 0
62
+ si = ((self.current_page - 1) * self.results_per_page)
63
+ return (si >= 0 ? si + 1 : 0)
64
+ end
65
+
66
+ # The ending index for this group of results.
67
+ # Useful for building things like:
68
+ # Displaying 11 - 20 of 56 results.
69
+ def end_index
70
+ ei = self.current_page * self.results_per_page
71
+ return (ei < self.total_results ? ei : self.total_results)
72
+ end
73
+
74
+ def ==(other) # :nodoc:
75
+ self.results == other.results
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+ end
@@ -8,7 +8,7 @@ class ScaffoldGenerator < Genosaurus
8
8
 
9
9
  def setup # :nodoc:
10
10
  @name_singular = param(:name).singular.underscore
11
- @name_plural = param(:name).plural.underscore
11
+ @name_plural = @name_singular.plural.underscore
12
12
  @name_singular_camel = @name_singular.camelcase
13
13
  @name_plural_camel = @name_plural.camelcase
14
14
  @test_framework = configatron.mack.testing_framework
@@ -1,6 +1,8 @@
1
1
  require 'rake'
2
2
  namespace :db do
3
3
 
4
+ task :init => [:recreate, :migrate]
5
+
4
6
  desc "Drops (if it exists) the database and then creates it for your environment."
5
7
  task :recreate => :environment do
6
8
  Mack::Database.drop(Mack.env, repis)
@@ -4,7 +4,7 @@ namespace :test do
4
4
  desc "Sets up your testing environment"
5
5
  task :setup do
6
6
  ENV["MACK_ENV"] = "test"
7
- # Mack.reset_logger!
7
+ Mack.reset_logger!
8
8
  Rake::Task["db:recreate"].invoke
9
9
  Mack::Database.dump_structure("development", :default)
10
10
  Mack::Database.load_structure(Mack::Paths.db("development_schema_structure.sql"))
@@ -18,7 +18,7 @@ namespace :test do
18
18
  if respond_to?(:run_factories)
19
19
  # auto require factories:
20
20
  Dir.glob(Mack::Paths.test("factories", "**/*.rb")).each do |f|
21
- require f
21
+ require File.expand_path(f)
22
22
  end
23
23
  run_factories(:init)
24
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mack-orm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0.101
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-11 00:00:00 -04:00
12
+ date: 2008-10-26 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,6 +27,7 @@ files:
27
27
  - lib/mack-orm/generators.rb
28
28
  - lib/mack-orm/genosaurus_helpers.rb
29
29
  - lib/mack-orm/model_column.rb
30
+ - lib/mack-orm/paginator.rb
30
31
  - lib/mack-orm/scaffold_generator/manifest.yml
31
32
  - lib/mack-orm/scaffold_generator/scaffold_generator.rb
32
33
  - lib/mack-orm/scaffold_generator/templates/app/controllers/controller.rb.template