ar_attr_lazy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.tmproj
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Elliot Winkler
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ # ar_attr_lazy
2
+
3
+ ## Summary
4
+
5
+ A little gem for Rails that provides the ability to specify attributes that will not be loaded when the record is loaded from the database, until you explicitly refer to those attributes. This is useful when you have a lot of text columns in your table; in this case lazy-loading the text attributes is a good way to lend your server a hand and cut down on database access time.
6
+
7
+ ## Installation/Usage
8
+
9
+ First:
10
+
11
+ 1. Run `gem install ar_attr_lazy` (probably as root)
12
+ 2. Add `config.gem 'ar_attr_lazy'` to environment.rb
13
+ 3. Optionally run `rake gems:unpack` to vendor the gem
14
+
15
+ Then, simply add an `attr_lazy` line to your model, listing the attributes you want lazy-loaded. For instance:
16
+
17
+ class Post < ActiveRecord::Base
18
+ attr_lazy :body
19
+ end
20
+
21
+ Now when you do a `find`, instead of doing a `SELECT *`, it does e.g. `SELECT id, permalink, title, created_at, updated_at`, and only when you say `post.body` will it pull the `body` column.
22
+
23
+ ## Support
24
+
25
+ If you find a bug or have a feature request, I want to know about it! Feel free to file a [Github issue](http://github.com/mcmire/ar_attr_lazy/issues), or do one better and fork the [project on Github](http://github.com/mcmire/ar_attr_lazy) and send me a pull request or patch. Be sure to add tests if you do so, though.
26
+
27
+ You can also [email me](mailto:elliot.winkler@gmail.com), or [find me on Twitter](http://twitter.com/mcmire).
28
+
29
+ ## Inspiration
30
+
31
+ <http://refactormycode.com/codes/219-activerecord-lazy-attribute-loading-plugin-for-rails>
32
+
33
+ ## Author/License
34
+
35
+ (c) 2009-2010 Elliot Winkler. See LICENSE for details.
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require File.dirname(__FILE__) + "/lib/mcmire/ar_attr_lazy/version"
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.version = Mcmire::ArAttrLazy::VERSION
10
+ gem.name = "ar_attr_lazy"
11
+ gem.summary = %Q{Rails gem that provides lazy-loading for attributes.}
12
+ gem.description = %Q{A little gem for Rails that provides the ability to specify attributes that will not be loaded when the record is loaded from the database, until you explicitly refer to those attributes. This is useful when you have a lot of text columns in your table; in this case lazy-loading the text attributes is a good way to lend your server a hand and cut down on database access time.}
13
+ gem.email = "elliot.winkler@gmail.com"
14
+ gem.homepage = "http://github.com/mcmire/ar_attr_lazy"
15
+ gem.authors = ["Elliot Winkler"]
16
+ unless ENV["AR_VERSION"]
17
+ gem.add_dependency "activerecord", "< 3.0"
18
+ end
19
+ gem.add_development_dependency "mcmire-protest"
20
+ gem.add_development_dependency "mcmire-matchy"
21
+ gem.add_development_dependency "mcmire-mocha"
22
+ gem.add_development_dependency "mocha-protest-integration"
23
+ #gem.add_development_dependency "yard", ">= 0"
24
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
25
+ end
26
+ Jeweler::GemcutterTasks.new
27
+ rescue LoadError
28
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
29
+ end
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ begin
39
+ require 'rcov/rcovtask'
40
+ Rcov::RcovTask.new do |test|
41
+ test.libs << 'test'
42
+ test.pattern = 'test/**/*_test.rb'
43
+ test.verbose = true
44
+ end
45
+ rescue LoadError
46
+ task :rcov do
47
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
48
+ end
49
+ end
50
+
51
+ task :test => [:check_dependencies, :"check_dependencies:development"]
52
+
53
+ task :default => :test
54
+
55
+ begin
56
+ require 'yard'
57
+ YARD::Rake::YardocTask.new
58
+ rescue LoadError
59
+ task :yardoc do
60
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
61
+ end
62
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'mcmire/ar_attr_lazy'
@@ -0,0 +1,32 @@
1
+ module Mcmire
2
+ module ArAttrLazy
3
+ class Version
4
+ include Comparable
5
+
6
+ def initialize(version_string)
7
+ @major, @minor, @tiny = split(version_string)
8
+ end
9
+
10
+ def <=>(other_version_string)
11
+ [@major, @minor, @tiny] <=> split(other_version_string)
12
+ end
13
+
14
+ private
15
+ def split(version_string)
16
+ pieces = version_string.to_s.split(".")
17
+ (0..2).map {|i| pieces[i].to_i || 0 }
18
+ end
19
+ end
20
+
21
+ def self.ar_version
22
+ @ar_version ||= Version.new(ActiveRecord::VERSION)
23
+ end
24
+ end
25
+ end
26
+
27
+ require 'mcmire/ar_attr_lazy/base_ext'
28
+ require 'mcmire/ar_attr_lazy/association_preload_ext'
29
+ require 'mcmire/ar_attr_lazy/habtm_ext'
30
+ require 'mcmire/ar_attr_lazy/join_base_ext'
31
+ require 'mcmire/ar_attr_lazy/belongs_to_association_ext'
32
+ require 'mcmire/ar_attr_lazy/has_many_through_association_ext'
@@ -0,0 +1,55 @@
1
+ module Mcmire
2
+ module ArAttrLazy
3
+ module AssociationPreloadExt
4
+ def self.included(includer)
5
+ includer.alias_method_chain :find_associated_records, :attr_lazy
6
+ end
7
+
8
+ #def preload_one_association(records, association, preload_options={})
9
+ # class_to_reflection = {}
10
+ # records.group_by {|record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, records|
11
+ # raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection
12
+ # # Add the unlazy columns as the default :select option (overrideable with an explicit select)
13
+ # if record = records[0] and record.class.respond_to?(:unlazy_column_list)
14
+ # preload_options[:select] = record.class.unlazy_column_list
15
+ # end
16
+ # send("preload_#{reflection.macro}_association", records, reflection, preload_options)
17
+ # end
18
+ #end
19
+
20
+ def find_associated_records_with_attr_lazy(ids, reflection, preload_options)
21
+ # Add the unlazy columns as the default :select option (overrideable with an explicit select)
22
+ preload_options[:select] = reflection.klass.unlazy_column_list if reflection.klass.respond_to?(:unlazy_column_list)
23
+ find_associated_records_without_attr_lazy(ids, reflection, preload_options)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ ActiveRecord::AssociationPreload::ClassMethods.class_eval do
30
+ include Mcmire::ArAttrLazy::AssociationPreloadExt
31
+
32
+ # Unfortunately we can't override this using a module...
33
+ def preload_has_and_belongs_to_many_association(records, reflection, preload_options={})
34
+ table_name = reflection.klass.quoted_table_name
35
+ id_to_record_map, ids = construct_id_map(records)
36
+ records.each {|record| record.send(reflection.name).loaded}
37
+ options = reflection.options
38
+
39
+ conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}"
40
+ conditions << append_conditions(reflection, preload_options)
41
+
42
+ associated_records = reflection.klass.with_exclusive_scope do
43
+ # Select unlazy_column_list by default
44
+ select = options[:select]
45
+ select ||= reflection.klass.unlazy_column_list if reflection.klass.respond_to?(:unlazy_column_list)
46
+ select ||= table_name+'.*'
47
+ reflection.klass.find(:all, :conditions => [conditions, ids],
48
+ :include => options[:include],
49
+ :joins => "INNER JOIN #{connection.quote_table_name options[:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}",
50
+ :select => "#{select}, t0.#{reflection.primary_key_name} as the_parent_record_id",
51
+ :order => options[:order])
52
+ end
53
+ set_association_collection_records(id_to_record_map, reflection.name, associated_records, 'the_parent_record_id')
54
+ end
55
+ end
@@ -0,0 +1,69 @@
1
+ module Mcmire
2
+ module ArAttrLazy
3
+ module BaseExt
4
+ module ClassMethods
5
+ def unlazy_column_names
6
+ column_names - attr_lazy_columns
7
+ end
8
+
9
+ def unlazy_column_list
10
+ unlazy_column_names.map {|c| "#{quoted_table_name}.#{connection.quote_column_name(c)}" }.join(",")
11
+ end
12
+
13
+ def find_with_attr_lazy(*args)
14
+ # don't limit :select clause if there aren't any lazy attributes defined on this model
15
+ # or we're inside a scope right now that has already defined :select
16
+ if attr_lazy_columns.empty? or (scope = scope(:find) and scope[:select])
17
+ find_without_attr_lazy(*args)
18
+ else
19
+ with_scope(:find => { :select => unlazy_column_list }) do
20
+ find_without_attr_lazy(*args)
21
+ end
22
+ end
23
+ end
24
+
25
+ def read_lazy_attribute(record, attr)
26
+ # we use with_exclusive_scope here to override any :includes that may have happened in a parent scope
27
+ select = [primary_key, attr].map {|c| "#{quoted_table_name}.#{connection.quote_column_name(c)}" }.join(",")
28
+ with_exclusive_scope(:find => { :select => select }) do
29
+ find_without_attr_lazy(record[primary_key])[attr]
30
+ end
31
+ end
32
+ end
33
+
34
+ module InstanceMethods
35
+ private
36
+ def read_lazy_attribute(attr)
37
+ attr = attr.to_s
38
+ unless @attributes.include?(attr)
39
+ @attributes[attr] = self.class.read_lazy_attribute(self, attr)
40
+ end
41
+ @attributes[attr]
42
+ end
43
+ end
44
+
45
+ module MacroMethods
46
+ def attr_lazy(*args)
47
+ include InstanceMethods
48
+ extend ClassMethods
49
+ class_inheritable_accessor :attr_lazy_columns
50
+ write_inheritable_attribute :attr_lazy_columns, []
51
+ (class << self; self; end).class_eval do
52
+ alias_method_chain :find, :attr_lazy
53
+ end
54
+
55
+ args = [args].flatten.map(&:to_s)
56
+ new_cols = args - (attr_lazy_columns & args)
57
+ write_inheritable_attribute(:attr_lazy_columns, attr_lazy_columns | args)
58
+ new_cols.each do |col|
59
+ class_eval("def #{col}; read_lazy_attribute :#{col}; end", __FILE__, __LINE__)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ ActiveRecord::Base.class_eval do
68
+ extend Mcmire::ArAttrLazy::BaseExt::MacroMethods
69
+ end
@@ -0,0 +1,19 @@
1
+ ActiveRecord::Associations::BelongsToAssociation.class_eval do
2
+ # Unfortunately we can't override this using a module...
3
+ def find_target
4
+ find_method = if @reflection.options[:primary_key]
5
+ "find_by_#{@reflection.options[:primary_key]}"
6
+ else
7
+ "find"
8
+ end
9
+ # Select unlazy_column_list by default
10
+ select = @reflection.klass.unlazy_column_list if @reflection.klass.respond_to?(:unlazy_column_list)
11
+ @reflection.klass.send(find_method,
12
+ @owner[@reflection.primary_key_name],
13
+ :select => select || @reflection.options[:select],
14
+ :conditions => conditions,
15
+ :include => @reflection.options[:include],
16
+ :readonly => @reflection.options[:readonly]
17
+ ) if @owner[@reflection.primary_key_name]
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module Mcmire
2
+ module ArAttrLazy
3
+ module HabtmExt
4
+ def self.included(includer)
5
+ includer.instance_eval do
6
+ alias_method_chain :construct_find_options!, :attr_lazy
7
+ end
8
+ end
9
+
10
+ def construct_find_options_with_attr_lazy!(options)
11
+ # Select unlazy_column_list by default
12
+ options[:select] ||= @reflection.options[:select]
13
+ options[:select] ||= @reflection.klass.unlazy_column_list if @reflection.klass.respond_to?(:unlazy_column_list)
14
+ options[:select] ||= '*'
15
+ construct_find_options_without_attr_lazy!(options)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
22
+ include Mcmire::ArAttrLazy::HabtmExt
23
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Associations::HasManyThroughAssociation.class_eval do
2
+ # Unfortunately we can't override this using a module...
3
+ def construct_select(custom_select = nil)
4
+ distinct = "DISTINCT " if @reflection.options[:uniq]
5
+ default_select = @reflection.klass.unlazy_column_list if @reflection.klass.respond_to?(:unlazy_column_list)
6
+ default_select ||= "#{@reflection.quoted_table_name}.*"
7
+ selected = custom_select || @reflection.options[:select] || "#{distinct}#{default_select}"
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase.class_eval do
2
+ # Unfortunately we can't override this using a module...
3
+ def column_names_with_alias
4
+ unless defined?(@column_names_with_alias)
5
+ @column_names_with_alias = []
6
+ # Use unlazy_column_names instead of just column_names
7
+ column_names = active_record.respond_to?(:unlazy_column_names) ? active_record.unlazy_column_names : active_record.column_names
8
+ ([active_record.primary_key] + (column_names - [active_record.primary_key])).each_with_index do |column_name, i|
9
+ @column_names_with_alias << [column_name, "#{ aliased_prefix }_r#{ i }"]
10
+ end
11
+ end
12
+ @column_names_with_alias
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Mcmire
2
+ module ArAttrLazy
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,96 @@
1
+ class FactoryMaker
2
+ class << self
3
+ attr_accessor :factories
4
+
5
+ # make(klass)
6
+ # make(klass, factory)
7
+ # make(klass, factory, custom_attributes)
8
+ def make(*args, &block)
9
+ factorize(:new, args, &block)
10
+ end
11
+
12
+ # make!(klass)
13
+ # make!(klass, factory)
14
+ # make!(klass, factory, custom_attributes)
15
+ def make!(*args, &block)
16
+ factorize(:create!, args, &block)
17
+ end
18
+
19
+ def attributes(factory)
20
+ attributes = FactoryMaker.factories[factory]
21
+ raise "Couldn't find factory \"#{factory}\"" unless attributes
22
+ attributes
23
+ end
24
+
25
+ private
26
+ def factorize(method, args, &block)
27
+ custom_attributes = args.extract_options!
28
+ klass, factory = args
29
+ factory ||= klass.to_s.underscore.to_sym
30
+ attributes = self.attributes(factory)
31
+ model = klass.send(method, attributes.merge(custom_attributes))
32
+ yield(model) if block_given?
33
+ model
34
+ end
35
+ end
36
+ end
37
+
38
+ class ActiveRecord::Base
39
+ class << self
40
+ # make
41
+ # make(factory)
42
+ # make(factory, custom_attributes)
43
+ def make(*args, &block)
44
+ factorize(:make, args, &block)
45
+ end
46
+
47
+ # make!
48
+ # make!(factory)
49
+ # make!(factory, custom_attributes)
50
+ def make!(*args, &block)
51
+ factorize(:make!, args, &block)
52
+ end
53
+
54
+ private
55
+ def factorize(method, args, &block)
56
+ custom_attributes = args.extract_options!
57
+ factory = args.first
58
+ FactoryMaker.send(method, self, factory, custom_attributes, &block)
59
+ end
60
+ end
61
+ end
62
+
63
+ FactoryMaker.factories = {
64
+ :account => {
65
+ :name => "Joe's Account"
66
+ },
67
+ :user => {
68
+ :name => "Joe Bloe",
69
+ :login => "joe",
70
+ :password => "secret",
71
+ :email => "joe@bloe.com",
72
+ :bio => "It's hip to be square!"
73
+ },
74
+ :avatar => {
75
+ :filename => "somefile.png",
76
+ :data => "10101010010010001000101"
77
+ },
78
+ :post => {
79
+ :title => "The Best Post",
80
+ :permalink => 'the-best-post',
81
+ :body => "This is the best post, ya hear?? Word.",
82
+ :summary => "T i t b p, y h?? W."
83
+ },
84
+ :comment => {
85
+ :name => "A douchebag",
86
+ :body => "Your site suxx0rsss"
87
+ },
88
+ :tag => {
89
+ :name => "Foo",
90
+ :description => "The description and stuff"
91
+ },
92
+ :category => {
93
+ :name => "zing",
94
+ :description => "Hey hey hey hey I don't like your girlfriend"
95
+ }
96
+ }