bootstripe-rails 0.2.1

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bootstripe-rails.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bootstripe-rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bootstripe-rails"
7
+ s.version = Bootstripe::Rails::VERSION
8
+ s.authors = ["Mike Laurence", "Jason Pearl"]
9
+ s.email = ["mike@bytebin.com", "jason@bytebin.com"]
10
+ s.homepage = "http://github.com/bytebin/bootstripe-rails"
11
+ s.summary = %q{A handful of useful additions for Rails}
12
+ s.description = %q{A handful of useful additions for Rails}
13
+
14
+ s.rubyforge_project = "bootstripe-rails"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency "sqlite3"
23
+ s.add_runtime_dependency "rails"
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'bootstripe-rails/version'
2
+
3
+ # Additions
4
+ require 'bootstripe-rails/model_additions'
5
+
6
+ # Validation
7
+ require 'bootstripe-rails/validators'
@@ -0,0 +1,46 @@
1
+ module ActiveRecord
2
+ class Base
3
+ # Options:
4
+ # :merge_lookup => true/false [defaults to true]
5
+ def self.upsert(lookup, params, options = {})
6
+ merge_lookup = options[:merge_lookup] || true
7
+ subject = nil
8
+
9
+ transaction do
10
+ subject = where(lookup).first
11
+
12
+ if subject
13
+ subject.update_attributes(params)
14
+ else
15
+ params = params.merge(lookup) if merge_lookup
16
+ subject = create(params)
17
+ end
18
+ end
19
+
20
+ subject
21
+ end
22
+
23
+ def self.loaded_models
24
+ ActiveRecord::Base.send(:descendants)
25
+ end
26
+
27
+ def self.randomize
28
+ if adapter = ActiveRecord::Base.connection.adapter_name
29
+ order(adapter.match(/postgres/i) ? 'RANDOM()' : 'RAND()')
30
+ else
31
+ raise 'No ActiveRecord adapter specified!'
32
+ end
33
+ end
34
+
35
+ def self.ordered
36
+ order 'created_at ASC'
37
+ end
38
+
39
+ def clone!(params = {})
40
+ cl = self.class.new
41
+ time = Time.current
42
+ cl.send :attributes=, HashWithIndifferentAccess.new(self.attributes).except(:id).merge(:created_at => time, :updated_at => time).merge(params), false
43
+ cl
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class ZipcodeValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ record.errors[attribute] << 'is not properly formatted' unless /^[0-9]{5}([- ]?[0-9]{4})?$/.match(value)
6
+ end
7
+ end
8
+
9
+ class EmailValidator < ActiveModel::EachValidator
10
+ def validate_each(record, attribute, value)
11
+ record.errors[attribute] << 'is not properly formatted' unless /^[A-Z0-9_\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)$/i.match(value)
12
+ end
13
+ end
14
+
15
+ class PhoneValidator < ActiveModel::EachValidator
16
+ def validate_each(record, attribute, value)
17
+ record.errors[attribute] << 'is not properly formatted' unless /1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?/.match(value)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Bootstripe
2
+ module Rails
3
+ VERSION = "0.2.1"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Base do
4
+
5
+ before(:each) do
6
+ User.destroy_all
7
+ User.create :name => 'Mike', :email => 'mike@mike.com', :password => 'pazzword'
8
+ User.create :name => 'Jason', :email => 'jason@jason.com', :password => 'pazzword'
9
+ end
10
+
11
+ it 'should randomize results' do
12
+ User.randomize.to_sql.should =~ /ORDER BY RAND\(\)/
13
+ end
14
+
15
+ it 'should upsert' do
16
+ User.upsert({:name => 'Mike'}, {:email => 'mikeypants@mike.com'})
17
+ User.count.should eq 2
18
+ User.where(:name => 'Mike').first.email.should eq 'mikeypants@mike.com'
19
+ end
20
+
21
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rails'
4
+ require 'active_record'
5
+
6
+ require 'bootstripe-rails'
7
+
8
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
+ ActiveRecord::Schema.define(:version => 1) do
10
+ create_table :users do |t|
11
+ t.string :name
12
+ t.string :email
13
+ t.string :password
14
+ end
15
+ end
16
+
17
+ class User < ActiveRecord::Base
18
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstripe-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Laurence
9
+ - Jason Pearl
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-12-19 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &70104038663440 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70104038663440
26
+ - !ruby/object:Gem::Dependency
27
+ name: sqlite3
28
+ requirement: &70104038662740 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70104038662740
37
+ - !ruby/object:Gem::Dependency
38
+ name: rails
39
+ requirement: &70104038651480 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *70104038651480
48
+ description: A handful of useful additions for Rails
49
+ email:
50
+ - mike@bytebin.com
51
+ - jason@bytebin.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - Rakefile
59
+ - bootstripe-rails.gemspec
60
+ - lib/bootstripe-rails.rb
61
+ - lib/bootstripe-rails/model_additions.rb
62
+ - lib/bootstripe-rails/validators.rb
63
+ - lib/bootstripe-rails/version.rb
64
+ - spec/model_additions_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: http://github.com/bytebin/bootstripe-rails
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project: bootstripe-rails
86
+ rubygems_version: 1.8.11
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: A handful of useful additions for Rails
90
+ test_files:
91
+ - spec/model_additions_spec.rb
92
+ - spec/spec_helper.rb