activerecord-tableless 0.1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ nbproject
2
+ tmp
3
+ *.gem
4
+ *emfile.lock
data/CHANGELOG CHANGED
@@ -1,4 +1,7 @@
1
- ActiveRecord::Tableless CHANGELOG
2
- ---------------------------------
1
+ 0.1.1 - Jul 21th 2009
2
+ * Method for bulk adding of columns
3
+ * Code cleaning
4
+ * Rename of the project
3
5
 
4
- 2008-05-01 - Initial code, rough around the edges.
6
+ 0.1.0 - May 1st 2008
7
+ * Initial code, rough around the edges.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in utf8_enforcer_workaround.gemspec
4
+ gemspec
data/README CHANGED
@@ -1,6 +1,34 @@
1
- ActiveRecord Tableless Models
2
- -----------------------------
1
+ ActiveRecord Tableless
2
+ ----------------------
3
3
 
4
- A single implementation of the ActiveRecord Tableless Model pattern for any
5
- Rails project or other Ruby project that uses ActiveRecord.
4
+ A single implementation of the ActiveRecord Tableless pattern for any Rails
5
+ project or other Ruby project that uses ActiveRecord.
6
+
7
+ Define a model like this:
8
+
9
+ class ContactMessage < ActiveRecord::Base
10
+ has_no_table
11
+ column :name, :string
12
+ column :email, :string
13
+ validates_presence_of :name, :email
14
+ end
15
+
16
+ You can now use the model in a view like this:
17
+
18
+ <%= form_for :message, @message do |f| %>
19
+ Your name: <%= f.text_field :name %>
20
+ Your email: <%= f.text_field :email %>
21
+ <% end %>
22
+
23
+ And in the controller:
24
+
25
+ def message
26
+ @message = ContactMessage.new
27
+ if request.post?
28
+ @message.attributes = params[:message]
29
+ if @message.valid?
30
+ # Process the message...
31
+ end
32
+ end
33
+ end
6
34
 
data/Rakefile CHANGED
@@ -1,14 +1,15 @@
1
- require 'rake/rdoctask'
1
+ require 'bundler/gem_tasks'
2
+ require 'rdoc/task'
2
3
 
3
4
  task :default do
4
5
  puts
5
- puts IO.read( File.dirname(__FILE__) + '/README' )
6
+ puts IO.read(File.join(File.dirname(__FILE__), 'README'))
6
7
  puts
7
8
  end
8
9
 
9
10
  Rake::RDocTask.new do |rd|
10
11
  rd.main = 'README'
11
- rd.rdoc_files.include( 'README', 'lib/*.rb', 'lib/**/*.rb' )
12
+ rd.rdoc_files.include('README', 'lib/*.rb', 'lib/**/*.rb')
12
13
  rd.rdoc_dir = 'rdoc'
13
- rd.title = 'ActiveRecord Tableless Models'
14
- end
14
+ rd.title = 'ActiveRecord Tableless'
15
+ end
@@ -0,0 +1,25 @@
1
+ # -*- ruby -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Jarl Friis", "Kenneth Kalmer", "Michal Zima"]
5
+ gem.email = ["jarl@softace.dk"]
6
+ gem.name = 'activerecord-tableless'
7
+ gem.summary = %q{A library for implementing tableless ActiveRecord models}
8
+ gem.description = %q{ActiveRecord Tableless Models provides a simple mixin for creating models that are not bound to the database. This approach is mostly useful for capitalizing on the features ActiveRecord::Validation}
9
+ gem.homepage = "https://github.com/softace/activerecord-tableless"
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.version = "1.0.1"
14
+ gem.has_rdoc = true
15
+ gem.extra_rdoc_files = %w( README CHANGELOG )
16
+ gem.rdoc_options.concat ['--main', 'README']
17
+
18
+ gem.require_paths = ["lib"]
19
+ gem.platform = Gem::Platform::RUBY
20
+
21
+ gem.add_dependency("activerecord", ">=3.2")
22
+
23
+ gem.add_development_dependency('bundler')
24
+ gem.add_development_dependency('rake')
25
+ end
data/init.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # For use by Railed and de-Railed applications alike
2
2
 
3
- require 'activerecord'
4
- require File.dirname(__FILE__) + '/lib/tableless'
3
+ require 'active_record'
4
+ require File.join(File.dirname(__FILE__), 'lib', 'activerecord-tableless')
@@ -0,0 +1,151 @@
1
+ # See #ActiveRecord::Tableless
2
+
3
+ module ActiveRecord
4
+
5
+ # = ActiveRecord::Tableless
6
+ #
7
+ # Allow classes to behave like ActiveRecord models, but without an associated
8
+ # database table. A great way to capitalize on validations. Based on the
9
+ # original post at http://www.railsweenie.com/forums/2/topics/724 (which seems
10
+ # to have disappeared from the face of the earth).
11
+ #
12
+ # = Example usage
13
+ #
14
+ # class ContactMessage < ActiveRecord::Base
15
+ #
16
+ # has_no_table
17
+ #
18
+ # column :name, :string
19
+ # column :email, :string
20
+ # column :message, :string
21
+ #
22
+ # end
23
+ #
24
+ # msg = ContactMessage.new( params[:msg] )
25
+ # if msg.valid?
26
+ # ContactMessageSender.deliver_message( msg )
27
+ # redirect_to :action => :sent
28
+ # end
29
+ #
30
+ module Tableless
31
+
32
+ def self.included( base ) #:nodoc:
33
+ base.send :extend, ActsMethods
34
+ end
35
+
36
+ module ActsMethods #:nodoc:
37
+
38
+ # A model that needs to be tableless will call this method to indicate
39
+ # it.
40
+ def has_no_table
41
+ # keep our options handy
42
+ class_attribute :tableless_options
43
+ self.tableless_options = {:columns => []}
44
+
45
+ # extend
46
+ extend ActiveRecord::Tableless::SingletonMethods
47
+ extend ActiveRecord::Tableless::ClassMethods
48
+
49
+ # include
50
+ include ActiveRecord::Tableless::InstanceMethods
51
+
52
+ # setup columns
53
+ end
54
+
55
+ def tableless?
56
+ false
57
+ end
58
+ end
59
+
60
+ module SingletonMethods
61
+
62
+ # Return the list of columns registered for the model. Used internally by
63
+ # ActiveRecord
64
+ def columns
65
+ tableless_options[:columns]
66
+ end
67
+
68
+ # Register a new column.
69
+ def column(name, sql_type = nil, default = nil, null = true)
70
+ tableless_options[:columns] << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
71
+ end
72
+
73
+ # Register a set of colums with the same SQL type
74
+ def add_columns(sql_type, *args)
75
+ args.each do |col|
76
+ column col, sql_type
77
+ end
78
+ end
79
+
80
+ %w(find create destroy).each do |m|
81
+ eval %{
82
+ def #{m}(*args)
83
+ logger.warn "Can't #{m} a Tableless object"
84
+ false
85
+ end
86
+ }
87
+ end
88
+
89
+ def tableless?
90
+ true
91
+ end
92
+ end
93
+
94
+ module ClassMethods
95
+
96
+ def from_query_string(query_string)
97
+ unless query_string.blank?
98
+ params = query_string.split('&').collect do |chunk|
99
+ next if chunk.empty?
100
+ key, value = chunk.split('=', 2)
101
+ next if key.empty?
102
+ value = value.nil? ? nil : CGI.unescape(value)
103
+ [ CGI.unescape(key), value ]
104
+ end.compact.to_h
105
+
106
+ new(params)
107
+ else
108
+ new
109
+ end
110
+ end
111
+
112
+ end
113
+
114
+ module InstanceMethods
115
+
116
+ def to_query_string(prefix = nil)
117
+ attributes.to_a.collect{|(name,value)| escaped_var_name(name, prefix) + "=" + escape_for_url(value) if value }.compact.join("&")
118
+ end
119
+
120
+ %w(save destroy).each do |m|
121
+ eval %{
122
+ def #{m}(*args)
123
+ logger.warn "Can't #{m} a Tableless object"
124
+ false
125
+ end
126
+ }
127
+ end
128
+
129
+ private
130
+
131
+ def escaped_var_name(name, prefix = nil)
132
+ prefix ? "#{URI.escape(prefix)}[#{URI.escape(name)}]" : URI.escape(name)
133
+ end
134
+
135
+ def escape_for_url(value)
136
+ case value
137
+ when true then "1"
138
+ when false then "0"
139
+ when nil then ""
140
+ else URI.escape(value.to_s)
141
+ end
142
+ rescue
143
+ ""
144
+ end
145
+
146
+ end
147
+
148
+ end
149
+ end
150
+
151
+ ActiveRecord::Base.send( :include, ActiveRecord::Tableless )
metadata CHANGED
@@ -1,67 +1,115 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: activerecord-tableless
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
8
+ - Jarl Friis
7
9
  - Kenneth Kalmer
10
+ - Michal Zima
8
11
  autorequire:
9
12
  bindir: bin
10
13
  cert_chain: []
11
-
12
- date: 2008-05-01 00:00:00 +02:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
14
+ date: 2012-09-07 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
16
17
  name: activerecord
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: "0"
23
- version:
24
- description: ActiveRecord Tableless Models provides a simple mixin for creating models that are not bound to the database. This approach is mostly useful for capitalizing on the features ActiveRecord::Validation
25
- email: kenneth.kalmer@gmail.com
18
+ requirement: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: '3.2'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '3.2'
32
+ - !ruby/object:Gem::Dependency
33
+ name: bundler
34
+ requirement: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ description: ActiveRecord Tableless Models provides a simple mixin for creating models
65
+ that are not bound to the database. This approach is mostly useful for capitalizing
66
+ on the features ActiveRecord::Validation
67
+ email:
68
+ - jarl@softace.dk
26
69
  executables: []
27
-
28
70
  extensions: []
29
-
30
- extra_rdoc_files:
71
+ extra_rdoc_files:
31
72
  - README
32
73
  - CHANGELOG
33
- files:
34
- - init.rb
35
- - lib/tableless.rb
36
- - Rakefile
37
- - README
74
+ files:
75
+ - .gitignore
38
76
  - CHANGELOG
39
- has_rdoc: true
40
- homepage: http://www.opensourcery.co.za/activerecordtableless/
77
+ - Gemfile
78
+ - README
79
+ - Rakefile
80
+ - activerecord-tableless.gemspec
81
+ - init.rb
82
+ - lib/activerecord-tableless.rb
83
+ homepage: https://github.com/softace/activerecord-tableless
84
+ licenses: []
41
85
  post_install_message:
42
- rdoc_options:
86
+ rdoc_options:
43
87
  - --main
44
88
  - README
45
- require_paths:
89
+ require_paths:
46
90
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- version: "0"
52
- version:
53
- required_rubygems_version: !ruby/object:Gem::Requirement
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
58
- version:
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ segments:
98
+ - 0
99
+ hash: 193372649810044937
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ segments:
107
+ - 0
108
+ hash: 193372649810044937
59
109
  requirements: []
60
-
61
- rubyforge_project: tablelessmodels
62
- rubygems_version: 1.0.1
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.24
63
112
  signing_key:
64
- specification_version: 2
113
+ specification_version: 3
65
114
  summary: A library for implementing tableless ActiveRecord models
66
115
  test_files: []
67
-
data/lib/tableless.rb DELETED
@@ -1,74 +0,0 @@
1
- # See #ActiveRecord::Tableless
2
-
3
- module ActiveRecord
4
-
5
- # = ActiveRecord::Tableless
6
- #
7
- # Allow classes to behave like ActiveRecord models, but without an associated
8
- # database table. A great way to capitalize on validations. Based on the
9
- # original post at http://www.railsweenie.com/forums/2/topics/724 (which seems
10
- # to have disappeared from the face of the earth).
11
- #
12
- # = Example usage
13
- #
14
- # class ContactMessage < ActiveRecord::Base
15
- #
16
- # has_no_table
17
- #
18
- # column :name, :string
19
- # column :email, :string
20
- # column :message, :string
21
- #
22
- # end
23
- #
24
- # msg = ContactMessage.new( params[:msg] )
25
- # if msg.valid?
26
- # ContactMessageSender.deliver_message( msg )
27
- # redirect_to :action => :sent
28
- # end
29
- #
30
- module Tableless
31
-
32
- def self.included( base ) #:nodoc:
33
- base.send( :extend, ClassMethods )
34
- end
35
-
36
- module ClassMethods #:nodoc:
37
-
38
- # A model that needs to be tableless will call this method to indicate
39
- # it.
40
- def has_no_table
41
- # keep our options handy
42
- write_inheritable_attribute(
43
- :tableless_options,
44
- :columns => []
45
- )
46
- class_inheritable_reader :tableless_options
47
-
48
- # extend
49
- extend ActiveRecord::Tableless::SingletonMethods
50
-
51
- # setup columns
52
- end
53
-
54
- end
55
-
56
- module SingletonMethods
57
-
58
- # Return the list of columns registered for the model. Used internally by
59
- # ActiveRecord
60
- def columns
61
- tableless_options[:columns]
62
- end
63
-
64
- # Register a new column.
65
- def column(name, sql_type = nil, default = nil, null = true)
66
- tableless_options[:columns] << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
67
- end
68
-
69
- end
70
-
71
- end
72
- end
73
-
74
- ActiveRecord::Base.send( :include, ActiveRecord::Tableless )