auto_increment 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ test/app/log/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in auto_increment.gemspec
4
+ gemspec
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+
5
+ require 'rake'
6
+ require 'rake/testtask'
7
+ require 'rake/rdoctask'
8
+
9
+ desc 'Run Devise unit tests.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "auto_increment/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "auto_increment"
7
+ s.version = AutoIncrement::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Felipe Diesel"]
10
+ s.email = ["felipediesel@gmail.com"]
11
+ s.homepage = "http://felipediesel.com"
12
+ s.summary = %q{Auto increment a string or integer field}
13
+ s.description = %q{Automaticaly increments a string or integer field in ActiveRecord.}
14
+
15
+ s.rubyforge_project = "auto_increment"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1 @@
1
+ require 'auto_increment/active_record'
@@ -0,0 +1,38 @@
1
+ require 'active_record'
2
+
3
+ module AutoIncrement
4
+ def auto_increment(options = {})
5
+ raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
6
+
7
+ default_options = { :column => :code, :scope => nil, :initial => 1 }
8
+ options = default_options.merge(options) unless options.nil?
9
+
10
+ options[:scope] = [options[:scope]] if options[:scope].class != Array
11
+
12
+ before_create "auto_increment_#{options[:column]}"
13
+
14
+ define_method "auto_increment_#{options[:column]}" do
15
+
16
+ query = self.class.scoped
17
+ options[:scope].each do |scope|
18
+ if scope.present? and respond_to?(scope)
19
+ query = query.where "#{scope} = ?", send(scope)
20
+ end
21
+ end
22
+
23
+ if options[:initial].class == String
24
+ max = query.select("#{options[:column]} max").order("LENGTH(#{options[:column]}) DESC, #{options[:column]} DESC").first
25
+ max = max.max if max.present?
26
+ else
27
+ max = query.maximum options[:column]
28
+ end
29
+
30
+ max = max.blank? ? options[:initial] : max.next
31
+
32
+ write_attribute options[:column], max
33
+ end
34
+ end
35
+ end
36
+
37
+ # Extend ActiveRecord's functionality
38
+ ActiveRecord::Base.send :extend, AutoIncrement
@@ -0,0 +1,3 @@
1
+ module AutoIncrement
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails', '3.0.4'
4
+
5
+ # Bundle edge Rails instead:
6
+ # gem 'rails', :git => 'git://github.com/rails/rails.git'
7
+
8
+ gem 'sqlite3'
9
+
10
+ # Use unicorn as the web server
11
+ # gem 'unicorn'
12
+
13
+ # Deploy with Capistrano
14
+ # gem 'capistrano'
15
+
16
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
17
+ # gem 'ruby-debug'
18
+ # gem 'ruby-debug19'
19
+
20
+ # Bundle the extra gems:
21
+ # gem 'bj'
22
+ # gem 'nokogiri'
23
+ # gem 'sqlite3-ruby', :require => 'sqlite3'
24
+ # gem 'aws-s3', :require => 'aws/s3'
25
+
26
+ # Bundle gems for the local environment. Make sure to
27
+ # put test-only gems in this group so their generators
28
+ # and rake tasks are available in development mode:
29
+ # group :development, :test do
30
+ # gem 'webrat'
31
+ # end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>App</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run App::Application
@@ -0,0 +1,42 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ # If you have a Gemfile, require the gems listed there, including any gems
6
+ # you've limited to :test, :development, or :production.
7
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
8
+
9
+ module App
10
+ class Application < Rails::Application
11
+ # Settings in config/environments/* take precedence over those specified here.
12
+ # Application configuration should go into files in config/initializers
13
+ # -- all .rb files in that directory are automatically loaded.
14
+
15
+ # Custom directories with classes and modules you want to be autoloadable.
16
+ # config.autoload_paths += %W(#{config.root}/extras)
17
+
18
+ # Only load the plugins named here, in the order given (default is alphabetical).
19
+ # :all can be used as a placeholder for all plugins not explicitly named.
20
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
21
+
22
+ # Activate observers that should always be running.
23
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
24
+
25
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
26
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
27
+ # config.time_zone = 'Central Time (US & Canada)'
28
+
29
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
30
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
31
+ # config.i18n.default_locale = :de
32
+
33
+ # JavaScript files you want as :defaults (application.js is always included).
34
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
35
+
36
+ # Configure the default encoding used in templates for Ruby 1.9.
37
+ config.encoding = "utf-8"
38
+
39
+ # Configure sensitive parameters which will be filtered from the log file.
40
+ config.filter_parameters += [:password]
41
+ end
42
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
5
+
6
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ App::Application.initialize!
@@ -0,0 +1,26 @@
1
+ App::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ App::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end
@@ -0,0 +1,35 @@
1
+ App::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Tell Action Mailer not to deliver emails to the real world.
24
+ # The :test delivery method accumulates sent emails in the
25
+ # ActionMailer::Base.deliveries array.
26
+ config.action_mailer.delivery_method = :test
27
+
28
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
29
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
30
+ # like if you have constraints or database-specific column types
31
+ # config.active_record.schema_format = :sql
32
+
33
+ # Print deprecation notices to the stderr
34
+ config.active_support.deprecation = :stderr
35
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
4
+
5
+ class AutoIncrementActiveRecordTest < ActiveRecord::TestCase
6
+ def setup
7
+ @account1 = Account.create :name => 'My Account'
8
+ @account2 = Account.create :name => 'My Second Account'
9
+ @user1 = @account1.users.create :name => 'Felipe'
10
+ @user2 = @account1.users.create :name => 'Sabrina'
11
+
12
+ @user_account2 = @account2.users.create :name => 'Daniel'
13
+
14
+ @user3 = @account1.users.create :name => 'Paulo'
15
+ @user3.letter_code = 'Z'
16
+ @user3.save
17
+
18
+ @user4 = @account1.users.create :name => 'Marlene'
19
+ @user4.save
20
+
21
+ @user5 = @account1.users.create :name => 'Patricia'
22
+ @user5.save
23
+
24
+ end
25
+
26
+ test "use initial" do
27
+ assert_equal 1, @account1.code
28
+
29
+ assert_equal 'A', @user1.letter_code
30
+ end
31
+
32
+ test "increments numbers" do
33
+ assert_equal 2, @account2.code
34
+ end
35
+
36
+ test "increments letters" do
37
+ assert_equal 'B', @user2.letter_code
38
+ end
39
+
40
+ test "do not increment outside scope" do
41
+ assert_equal 'A', @user_account2.letter_code
42
+ end
43
+
44
+ test "checks if Z increments to AA" do
45
+ assert_equal 'AA', @user4.letter_code
46
+ end
47
+
48
+ test "checks if AA increments to AB" do
49
+ assert_equal 'AB', @user5.letter_code
50
+ end
51
+
52
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ ENV["RAILS_ENV"] = "test"
4
+ require File.expand_path('../app/config/environment', __FILE__)
5
+ require 'rails/test_help'
6
+
7
+ require File.expand_path('../../lib/auto_increment', __FILE__)
8
+
9
+
10
+ config = YAML.load_file(File.dirname(__FILE__) + '/database.yml')
11
+ ActiveRecord::Base.establish_connection(config['test'])
12
+
13
+ ActiveRecord::Base.connection.create_table :accounts do |t|
14
+ t.string :name
15
+ t.date :code
16
+ end
17
+
18
+ ActiveRecord::Base.connection.create_table :users do |t|
19
+ t.string :name
20
+ t.integer :account_id
21
+ t.string :letter_code
22
+ end
23
+
24
+
25
+ class Account < ActiveRecord::Base
26
+ auto_increment
27
+
28
+ has_many :users
29
+ end
30
+
31
+ class User < ActiveRecord::Base
32
+ auto_increment :column => :letter_code, :scope => :account_id, :initial => 'A'
33
+
34
+ belongs_to :account
35
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_increment
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Felipe Diesel
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-20 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Automaticaly increments a string or integer field in ActiveRecord.
22
+ email:
23
+ - felipediesel@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - auto_increment.gemspec
35
+ - lib/auto_increment.rb
36
+ - lib/auto_increment/active_record.rb
37
+ - lib/auto_increment/version.rb
38
+ - test/app/Gemfile
39
+ - test/app/app/controllers/application_controller.rb
40
+ - test/app/app/helpers/application_helper.rb
41
+ - test/app/app/views/layouts/application.html.erb
42
+ - test/app/config.ru
43
+ - test/app/config/application.rb
44
+ - test/app/config/boot.rb
45
+ - test/app/config/environment.rb
46
+ - test/app/config/environments/development.rb
47
+ - test/app/config/environments/production.rb
48
+ - test/app/config/environments/test.rb
49
+ - test/auto_increment_test.rb
50
+ - test/test_helper.rb
51
+ has_rdoc: true
52
+ homepage: http://felipediesel.com
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: auto_increment
81
+ rubygems_version: 1.5.0
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Auto increment a string or integer field
85
+ test_files:
86
+ - test/app/Gemfile
87
+ - test/app/app/controllers/application_controller.rb
88
+ - test/app/app/helpers/application_helper.rb
89
+ - test/app/app/views/layouts/application.html.erb
90
+ - test/app/config.ru
91
+ - test/app/config/application.rb
92
+ - test/app/config/boot.rb
93
+ - test/app/config/environment.rb
94
+ - test/app/config/environments/development.rb
95
+ - test/app/config/environments/production.rb
96
+ - test/app/config/environments/test.rb
97
+ - test/auto_increment_test.rb
98
+ - test/test_helper.rb