webget_ruby_rails 1.7.4

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig ADDED
Binary file
data/LICENSE.txt ADDED
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+
3
+ You may choose any of these licenses:
4
+
5
+ - CreativeCommons License, Non-commercial Share Alike
6
+ - LGPL, GNU Lesser General Public License
7
+ - MIT License
8
+ - Ruby License
9
+
10
+ THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
11
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+
2
+ = WebGet Ruby Gem: Rails extensions
3
+
4
+ Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
5
+ Copyright:: Copyright (c) 2009-2010 Joel Parker Henderson
6
+ License:: CreativeCommons License, Non-commercial Share Alike
7
+ License:: LGPL, GNU Lesser General Public License
8
+
9
+ Rails extensions are for ActiveRecord, etc.
10
+
11
+ Testing:
12
+ <ul>
13
+ <li>The easy way to run the tests: gem install webget_ruby_rails --test
14
+ <li>Some of the ActiveRecord extensions use sqlite for testing. We don't install sqlite automatically because it requires some native extensions. If you need sqlite: gem install sqlite3-ruby
15
+ </ul>
16
+
17
+
18
+ == ActiveRecord
19
+
20
+ * create_or_update_by: create a record, or update a record if value passed matches a field (or fields) in the AR object; includes method_missing function to make code more readable.
21
+ * seed: syntactic sugar alias for #create_or_update_by
22
+
23
+
24
+ == ActiveRecord::ConnectionAdapters::SchemaStatements
25
+
26
+ * add_column_and_index: database migration helper to add a table column and index at the same time.
27
+ * remove_column_and_index: database migration helper to add a table column and index at the same time.
28
+
29
+
30
+ == ActiveRecord::SaveExtensions
31
+
32
+ * save_false_then_reload!: a transaction to save and reload a record, to help repair associations
33
+
34
+
35
+ == Changes
36
+
37
+ - 1.0.0 Original
38
+
@@ -0,0 +1,45 @@
1
+ =begin rdoc
2
+
3
+ = WebGet Ruby Gem: Rails extensions
4
+
5
+ Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
6
+ Copyright:: Copyright (c) 2009-2010 Joel Parker Henderson
7
+ License:: CreativeCommons License, Non-commercial Share Alike
8
+ License:: LGPL, GNU Lesser General Public License
9
+
10
+ Rails extensions are for ActiveRecord, etc.
11
+
12
+ Testing:
13
+ <ul>
14
+ <li>The easy way to run the tests: gem install webget_ruby_rails --test
15
+ <li>Some of the ActiveRecord extensions use sqlite for testing. We don't install sqlite automatically because it requires some native extensions. If you need sqlite: gem install sqlite3-ruby
16
+ </ul>
17
+
18
+
19
+ == ActiveRecord
20
+
21
+ * create_or_update_by: create a record, or update a record if value passed matches a field (or fields) in the AR object; includes method_missing function to make code more readable.
22
+ * seed: syntactic sugar alias for #create_or_update_by
23
+
24
+
25
+ == ActiveRecord::ConnectionAdapters::SchemaStatements
26
+
27
+ * add_column_and_index: database migration helper to add a table column and index at the same time.
28
+ * remove_column_and_index: database migration helper to add a table column and index at the same time.
29
+
30
+
31
+ == ActiveRecord::SaveExtensions
32
+
33
+ * save_false_then_reload!: a transaction to save and reload a record, to help repair associations
34
+
35
+
36
+ == Changes
37
+
38
+ - 1.0.0 Original
39
+
40
+ =end
41
+
42
+ %w{active_record active_record/save_extensions}.map{|x|
43
+ require File.dirname(__FILE__) + "/webget_ruby_rails/#{x}.rb"
44
+ }
45
+
@@ -0,0 +1,119 @@
1
+ require 'activerecord'
2
+ require 'active_record'
3
+
4
+ #:startdoc:
5
+ # ActiveRecord extensions
6
+
7
+ module ActiveRecord #:doc:
8
+
9
+ class Base #:doc:
10
+
11
+ # Create a record, or update a record if value passed matches a field in the AR object;
12
+ # includes method_missing function to make code more readable
13
+ #
14
+ # Most common use will be for testing (fixture/mock object generation)
15
+ #
16
+ # Three versions of method included:
17
+ # create_or_update
18
+ # create_or_update_by
19
+ # create_or_update_by_xxx (where xxx is a field name)
20
+ #
21
+ # Inspired by http://www.intridea.com/2008/2/19/activerecord-base-create_or_update-on-steroids-2
22
+ #
23
+ # ==Example
24
+ # { "admin" => ["Administrator", 1000],
25
+ # "member" => ["Member", 1],
26
+ # "moderator" => ["Moderator", 100],
27
+ # "disabled" => ["Disabled User", -1] }.each_pair do |key, val|
28
+ # Role.create_or_update_by_key(:key => key, :name => val[0], :value => val[1])
29
+ # end
30
+
31
+ def self.create_or_update(options = {})
32
+ self.create_or_update_by(:id, options)
33
+ end
34
+
35
+
36
+ # Create or update a record by field (or fields).
37
+ # This will look for each field name as a key in the options hash.
38
+ #
39
+ # ==Example
40
+ # attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
41
+ # User.create_or_update_by(:email,attributes)
42
+ # => if a user with that email exists then update his name and birthdate, else create him
43
+ #
44
+ # ==Example with multiple conditions
45
+ # attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
46
+ # User.create_or_update_by([:name,:birthdate],attributes)
47
+ # => if a user with that name and birthdate exists then update his email, else create him
48
+ #
49
+ # The fields can be any mix of symbols or strings.
50
+ # The option keys can be any mix of symbols or strings.
51
+
52
+ def self.create_or_update_by(condition_keys, attribute_pairs = {})
53
+ condition_pairs=[*condition_keys].map{|key| [key,(attribute_pairs.delete(key)||attribute_pairs.delete(key.to_s)||attribute_pairs.delete(key.to_sym))]}.to_h
54
+ record = find(:first, :conditions => condition_pairs) || self.new
55
+ if record.new_record? then attribute_pairs.merge!(condition_pairs) end
56
+ attribute_pairs.each_pair{|key,val| record.send key.to_s+'=', val}
57
+ record.save!
58
+ return record
59
+ end
60
+
61
+
62
+ # Set up a database with initial data, e.g. in rake db:seed method.
63
+ #
64
+ # This will look for each field name as a key in the options hash.
65
+ #
66
+ # This method calls #create_or_update_by (and you may want to change
67
+ # this behavior to do more, e.g. to test that a DB and table exists).
68
+ #
69
+ # ==Example
70
+ # attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
71
+ # User.create_or_update_by(:email,attributes)
72
+ # => if a user with that email exists then update his name and birthdate, else create him
73
+ #
74
+ # ==Example with multiple conditions
75
+ # attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
76
+ # User.create_or_update_by([:name,:birthdate],attributes)
77
+ # => if a user with that name and birthdate exists then update his email, else create him
78
+ #
79
+ # The fields can be any mix of symbols or strings.
80
+ # The option keys can be any mix of symbols or strings.
81
+
82
+ def self.seed(condition_keys, attribute_pairs = {})
83
+ self.create_or_update_by(condition_keys, attribute_pairs)
84
+ end
85
+
86
+
87
+ # Method missing for create_or_update_by_xxx that forwards to #create_or_update_by
88
+ #
89
+ # ==Example
90
+ # MyModel.create_or_update_by_foo(options)
91
+ # => MyModel.create_or_update_by(:foo,option)
92
+
93
+ def self.method_missing_with_create_or_update(method_name, *args)
94
+ if match = method_name.to_s.match(/create_or_update_by_([a-z0-9_]+)/)
95
+ field = match[1]
96
+ return create_or_update_by(field,*args)
97
+ end
98
+ return method_missing_without_create_or_update(method_name, *args)
99
+ end
100
+
101
+
102
+ # For alias method chain
103
+ def self.included(base)
104
+ # base == ActiveRecord::Base (the class)
105
+ base.class_eval do
106
+ # class_eval makes self == ActiveRecord::Base, and makes def define instance methods.
107
+ extend ClassMethods
108
+ # If has_many were an instance method, we could do this
109
+ # alias_method_chain :has_many, :association_option; end
110
+ # but it's a class method, so we have to do the alias_method_chain on
111
+ # the meta-class for ActiveRecord::Base, which is what class << self does.
112
+ class << self; alias_method_chain :method_missing, :create_or_update; end
113
+ end
114
+ end
115
+
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,24 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module SchemaStatements
4
+
5
+ # Add a column and its index.
6
+ # This just calls #add_column then #add_index
7
+
8
+ def add_column_and_index(table_name, column_name, type, options = {})
9
+ add_column(table_name, column_name, type, options)
10
+ add_index(table_name, column_name, type, options)
11
+ end
12
+
13
+
14
+ # Remove a column and its index in one step.
15
+ # This just calls #remove_column then #remove_index.
16
+
17
+ def remove_column_and_index(table_name, column_name, type, options = {})
18
+ remove_column(table_name, column_name, type, options)
19
+ remove_index(table_name, column_name, type, options)
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ module ActiveRecord::SaveExtensions
2
+
3
+ # Save the record without validation, then reload it.
4
+ # If the record is valid then return true, otherwise raise RecordInvalid.
5
+ # This solves an issue we found with Rails associations not saving.
6
+ #
7
+ # By Andrew Carpenter (acarpen@wested.org)
8
+ #
9
+ # Deprecated, superceded by #save_redux!
10
+
11
+ def save_false_then_reload!
12
+ transaction do
13
+ save(false)
14
+ reload
15
+ valid? or raise ActiveRecord::RecordInvalid.new(self)
16
+ end
17
+ return true
18
+ end
19
+
20
+
21
+ # Save the record without validation, then reload, then save with validtion.
22
+ # This ensure that rails brings back the correct associations to validate.
23
+ # This solves an issue we found with Rails associations not saving.
24
+
25
+ def save_redux!
26
+ transaction do
27
+ save(false)
28
+ reload
29
+ save!
30
+ end
31
+ return true
32
+ end
33
+
34
+ end
35
+ ActiveRecord::Base.send(:include, ActiveRecord::SaveExtensions)
@@ -0,0 +1,9 @@
1
+ require 'test/unit'
2
+ require 'webget_ruby_rails'
3
+
4
+ class SchemaStatementsTest < Test::Unit::TestCase
5
+
6
+ def test_placeholder
7
+ end
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+ require 'webget_ruby_rails'
3
+ require 'active_record'
4
+
5
+ class SaveExtensionsText < Test::Unit::TestCase
6
+ end
7
+
@@ -0,0 +1,64 @@
1
+ require 'test/unit'
2
+ require 'webget_ruby_rails'
3
+ require 'active_record'
4
+
5
+ begin
6
+ require 'sqlite3'
7
+ rescue
8
+ # noop because sqlite may already be available,
9
+ # e.g. in JRuby on Ubuntu you can install it:
10
+ # apt-get install libsqlite3-ruby
11
+ end
12
+
13
+ begin
14
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
15
+ rescue
16
+ raise ""+
17
+ "ActiveRecord cannot establish connection to sqlite3 database memory.\n" +
18
+ "Please verify that you have the sqlite3 database installed for testing.\n" +
19
+ "To install it for Ruby typically do: sudo gem install sqlite3-ruby\n" +
20
+ "To install it for JRuby + Ubuntu do: sudo apt-get install libsqlite3-ruby"
21
+ end
22
+
23
+ ActiveRecord::Schema.define(:version => 1) do
24
+ create_table :foos do |t|
25
+ t.string :a
26
+ t.string :b
27
+ t.string :c
28
+ end
29
+ end
30
+
31
+ class Foo < ActiveRecord::Base
32
+ end
33
+
34
+ class ActiveRecordTest < Test::Unit::TestCase
35
+
36
+ def test_prelim_count
37
+ assert_equal(0,Foo.count)
38
+ end
39
+
40
+ def test_prelim_create
41
+ f=Foo.new
42
+ f.a='aa'
43
+ f.b='bb'
44
+ f.c='cc'
45
+ f.save
46
+ assert_equal(1,Foo.count)
47
+ end
48
+
49
+ def test_seed_with_create
50
+ n1=Foo.count
51
+ Foo.seed(:a,{:a=>'xxx',:b=>'yyy'})
52
+ n2=Foo.count
53
+ assert_equal(n1+1,n2)
54
+ end
55
+
56
+ def test_seed_with_update
57
+ n1=Foo.count
58
+ f1=Foo.find(:first)
59
+ Foo.seed(:a,{:a=>f1.a,:b=>'bbb'})
60
+ n2=Foo.count
61
+ assert_equal(n1,n2)
62
+ end
63
+
64
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webget_ruby_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.7.4
5
+ platform: ruby
6
+ authors:
7
+ - WebGet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDvDCCAyWgAwIBAgIJAIlSqEkDQaZIMA0GCSqGSIb3DQEBBQUAMIGbMQswCQYD
14
+ VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZyYW5j
15
+ aXNjbzETMBEGA1UEChMKV2ViR2V0LmNvbTETMB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6tk628N
31
+ BHokpc4Kz63xSXqzYTnBS7rFBwlYThtNalQeWmoUjGh3Z0ZR0JlhU0ln8899LuJ3
32
+ DXnLFY0cVuBnNDMOOFl8vk1qIcZjcTovhzgcixpG6Uk5qmUsKHRLQf4oQJx7TfLK
33
+ -----END CERTIFICATE-----
34
+
35
+ date: 2010-02-19 00:00:00 -08:00
36
+ default_executable:
37
+ dependencies:
38
+ - !ruby/object:Gem::Dependency
39
+ name: webget_ruby_ramp
40
+ type: :runtime
41
+ version_requirement:
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.7.4
47
+ version:
48
+ description:
49
+ email: webget@webget.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ files:
57
+ - README.rdoc
58
+ - LICENSE.txt
59
+ - lib/webget_ruby_rails.rb
60
+ - lib/webget_ruby_rails/active_record.rb
61
+ - lib/webget_ruby_rails/active_record/connection_adapters/abstract/schema_statements.rb
62
+ - lib/webget_ruby_rails/active_record/save_extensions.rb
63
+ has_rdoc: true
64
+ homepage: http://webget.com/
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.5
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: "WebGet Ruby Gem: Rails extensions"
91
+ test_files:
92
+ - test/webget_ruby_rails/active_record_test.rb
93
+ - test/webget_ruby_rails/active_record/connection_adapters/abstract/schema_statements_test.rb
94
+ - test/webget_ruby_rails/active_record/save_extensions_test.rb
metadata.gz.sig ADDED
Binary file