activerecord-denormalize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record-denormalize.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Keita Urashima
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # ActiveRecord::Denormalize
2
+
3
+ Denormalize fields in ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activerecord-denormalize'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activerecord-denormalize
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ class Message < ActiveRecord::Base
23
+ # sender_type:string, sender_id:integer, _sender:hstore
24
+
25
+ belongs_to :sender, polymorphic: true
26
+ end
27
+
28
+ class User < ActiveRecord::Base
29
+ # name:string
30
+
31
+ has_many :messages, as: :sender, denormalize: {
32
+ attributes: [:name]
33
+ }
34
+ end
35
+
36
+ user = User.create!(name: 'foo')
37
+ message = Message.create!(sender: user)
38
+
39
+ message.sender_name #=> 'foo'
40
+
41
+ user.update_attribute :name, 'bar'
42
+
43
+ message.reload
44
+ message.sender_name #=> 'bar'
45
+
46
+ Message.all.each do |msg|
47
+ # no N+1 query
48
+ msg.sender_name
49
+ end
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'activerecord-denormalize/version'
7
+
8
+ Gem::Specification.new do |gem|
9
+ gem.name = 'activerecord-denormalize'
10
+ gem.version = ActiveRecord::Denormalize::VERSION
11
+ gem.authors = ['Keita Urashima']
12
+ gem.email = ['ursm@ursm.jp']
13
+ gem.description = %q{Denormalize fields in ActiveRecord.}
14
+ gem.summary = %q{}
15
+ gem.homepage = ""
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ['lib']
21
+
22
+ gem.add_dependency 'activerecord'
23
+ gem.add_dependency 'activerecord-postgres-hstore'
24
+
25
+ gem.add_development_dependency 'rspec'
26
+ end
@@ -0,0 +1,94 @@
1
+ require 'activerecord-postgres-hstore'
2
+
3
+ module ActiveRecord::Denormalize
4
+ class << self
5
+ def included(builder)
6
+ builder.valid_options << :denormalize
7
+ end
8
+
9
+ def extract_attributes(obj, keys)
10
+ return {} unless obj
11
+
12
+ keys.each_with_object({}) {|key, hash|
13
+ case key
14
+ when Hash
15
+ key.each do |k, v|
16
+ hash[k] = v.is_a?(Proc) ? obj.instance_exec(&v) : obj.__send__(v)
17
+ end
18
+ else
19
+ hash[key] = obj.__send__(key)
20
+ end
21
+ }
22
+ end
23
+ end
24
+
25
+ def build
26
+ super.tap {|reflection|
27
+ configure_denormalize reflection
28
+ }
29
+ end
30
+
31
+ private
32
+
33
+ def configure_denormalize(reflection)
34
+ return unless opts = options[:denormalize]
35
+
36
+ dest_class = reflection.klass
37
+ source_name = options[:as] || reflection.source_reflection.options[:as]
38
+
39
+ attributes = opts[:attributes]
40
+ store_in = opts[:store_in] || "_#{source_name}"
41
+ prefix = opts[:prefix] || "#{source_name}_"
42
+
43
+ define_denormalize_to_method attributes, store_in
44
+ define_denormalize_from_method dest_class, source_name, attributes, store_in
45
+ define_denormalize_readers dest_class, attributes, prefix, store_in
46
+ end
47
+
48
+ def define_denormalize_to_method(attributes, store_in)
49
+ dest_name = name
50
+ method_name = "denormalize_to_#{dest_name}"
51
+
52
+ mixin.redefine_method method_name do
53
+ attrs = ActiveRecord::Denormalize.extract_attributes(self, attributes)
54
+
55
+ __send__(dest_name).update_all ["#{store_in} = ?::hstore", attrs.to_hstore]
56
+ end
57
+
58
+ model.after_save method_name
59
+ end
60
+
61
+ def define_denormalize_from_method(dest_class, source_name, attributes, store_in)
62
+ method_name = "denormalize_from_#{source_name}"
63
+
64
+ dest_class.redefine_method method_name do
65
+ source = __send__(source_name)
66
+ attrs = ActiveRecord::Denormalize.extract_attributes(source, attributes)
67
+
68
+ self[store_in] = attrs.stringify_keys
69
+ end
70
+
71
+ dest_class.before_create method_name
72
+ end
73
+
74
+ def define_denormalize_readers(dest_class, attributes, prefix, store_in)
75
+ attributes.each do |attr|
76
+ case attr
77
+ when Hash
78
+ attr.each_key do |key|
79
+ define_denormalize_reader dest_class, key, prefix, store_in
80
+ end
81
+ else
82
+ define_denormalize_reader dest_class, attr, prefix, store_in
83
+ end
84
+ end
85
+ end
86
+
87
+ def define_denormalize_reader(dest_class, name, prefix, store_in)
88
+ dest_class.class_eval <<-RUBY
89
+ def #{prefix}#{name}
90
+ #{store_in}[#{name.to_s.inspect}]
91
+ end
92
+ RUBY
93
+ end
94
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+
3
+ require 'activerecord-denormalize/version'
4
+ require 'active_record/denormalize'
5
+
6
+ class ActiveRecord::Associations::Builder::HasMany
7
+ include ActiveRecord::Denormalize
8
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module Denormalize
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Denormalize do
4
+ subject(:message) { Message.create!(sender: sender) }
5
+
6
+ context 'normal case' do
7
+ let(:sender) { User.create!(name: 'foo') }
8
+
9
+ its(:sender_name) { should == sender.name }
10
+
11
+ specify do
12
+ expect {
13
+ sender.update_attribute :name, 'bar'
14
+ }.to change { message.reload.sender_name }.to('bar')
15
+ end
16
+ end
17
+
18
+ context 'not associated' do
19
+ let(:sender) { nil }
20
+
21
+ its(:sender_name) { should be_nil }
22
+ end
23
+ end
@@ -0,0 +1,61 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift File.dirname(__FILE__)
3
+
4
+ require 'bundler/setup'
5
+
6
+ require 'rspec'
7
+ require 'rspec/autorun'
8
+
9
+ require 'active_record'
10
+ require 'activerecord-postgres-hstore/activerecord'
11
+
12
+ RSpec.configure do |config|
13
+ config.around do |example|
14
+ ActiveRecord::Base.transaction do
15
+ example.run
16
+ raise ActiveRecord::Rollback
17
+ end
18
+ end
19
+ end
20
+
21
+ config = {
22
+ adapter: 'postgresql',
23
+ database: 'activerecord-denormalize',
24
+ username: 'postgres',
25
+ min_messages: 'warning'
26
+ }
27
+
28
+ ActiveRecord::Base.establish_connection config.merge(database: 'postgres', schema_search_path: 'public')
29
+ ActiveRecord::Base.connection.recreate_database config[:database]
30
+ ActiveRecord::Base.establish_connection config
31
+
32
+ class SetupMigration < ActiveRecord::Migration
33
+ def up
34
+ execute 'CREATE EXTENSION IF NOT EXISTS hstore'
35
+
36
+ create_table :messages do |t|
37
+ t.references :sender, polymorphic: true
38
+ t.hstore :_sender
39
+ end
40
+
41
+ create_table :users do |t|
42
+ t.string :name
43
+ end
44
+ end
45
+ end
46
+
47
+ ActiveRecord::Migration.verbose = false
48
+ SetupMigration.new.up
49
+
50
+ require 'activerecord-denormalize'
51
+
52
+ class Message < ActiveRecord::Base
53
+ belongs_to :sender, polymorphic: true
54
+ serialize :_sender, ActiveRecord::Coders::Hstore
55
+ end
56
+
57
+ class User < ActiveRecord::Base
58
+ has_many :messages, as: :sender, denormalize: {
59
+ attributes: [:name]
60
+ }
61
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-denormalize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keita Urashima
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord-postgres-hstore
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Denormalize fields in ActiveRecord.
63
+ email:
64
+ - ursm@ursm.jp
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - activerecord-denormalize.gemspec
75
+ - lib/active_record/denormalize.rb
76
+ - lib/activerecord-denormalize.rb
77
+ - lib/activerecord-denormalize/version.rb
78
+ - spec/active_record/denormalize_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: ''
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: ''
104
+ test_files:
105
+ - spec/active_record/denormalize_spec.rb
106
+ - spec/spec_helper.rb