sti_friendly 0.0.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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0be14fbf844ea9a1fe8384542c9c02467413713
4
+ data.tar.gz: 84d58327e69bcb03e098346f5c6b57c1b8d7aa51
5
+ SHA512:
6
+ metadata.gz: 5119cb323dc67cc51bd86efce1232ddd1586216e0a881569b3359b5ab95ef274d1c0d535d7c0291c5e6dc01e00d5f486101101d9bf02183fd784ceadc58a0c8d
7
+ data.tar.gz: 53b2a557c194c1b4435ffbfc3d2e086df27c7ac364183af9b03d1796a2ca2f76ee649d2b7eebbf6d471326f700bfa86f53179a6e8ff49f6b20a2100a770207ea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Stanislav Gordanov.
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.
@@ -0,0 +1,51 @@
1
+ # StiFriendly
2
+
3
+ **Дано**: *Table id: 1, type: 'OldSubclass'*
4
+
5
+ При обновлении STI модели, обновляемая строка идентифицируется не только по её id, но ещё и по её классу.
6
+ При обновлении типа (класса) у записи с такими атрибутами {id: 1, typе 'OldSublclass'} на 'NewSubclass' рельсы сделают следующий запрос:
7
+
8
+ ```sql
9
+ UPDATE tables SET type='NewSubclass' WHERE type IN 'NewSubclass' AND id=1
10
+ ```
11
+
12
+ **Проблема**: До обновления в базе ещё нет записи с id=1 и type='NewSubclass'.
13
+
14
+ - Дополнительное описание проблемы: http://stackoverflow.com/questions/16655543/sti-in-rails-how-do-i-change-from-a-superclass-to-a-subclass-without-accessing
15
+ - Решение обновления типа через второй запрос: http://wegowise.github.io/blog/2013/05/09/rails-gotcha-saving-sti-records/
16
+ - Решение на основе перекрытия метода update: http://blog.arkency.com/2013/07/sti/
17
+ - Примененное решение сделано на основе замечания "If you want to disable Single Table Inheritance or use the type column for something else, you can use self.inheritance_column = :fake_column." в http://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-1/
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'sti_friendly'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install sti_friendly
34
+
35
+ ## Setup
36
+
37
+ ```ruby
38
+ module Offers
39
+ class Custom < ActiveRecord::Base
40
+ include StiFriendly
41
+ end
42
+ end
43
+ ```
44
+
45
+ ## Contributing
46
+ 1. [Fork it](https://github.com/abak-press/sti_friendly/fork)
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Write code _and_ tests
49
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 5. Push to the branch (`git push origin my-new-feature`)
51
+ 6. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,35 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+ require 'sti_friendly/version'
4
+
5
+ module StiFriendly
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def inherited(base)
10
+ super
11
+ base.send :include, Module.new { base.inheritance_column = :nil }
12
+ end
13
+ end
14
+
15
+ # Public: Замена одного класса на другой с сохранением аттрибутов предыдущего экземпляра класса.
16
+ #
17
+ # Фиксирование becomes версии 3.1 рельсов:
18
+ # http://apidock.com/rails/v3.1.0/ActiveRecord/Persistence/becomes
19
+ #
20
+ # В рельсах более поздних версий, было добавлено копирование ошибок, определение которых
21
+ # основывается не на текущем экземпляре, а на том, что находится в @base у Errors, а он остаётся прежним.
22
+ #
23
+ # klass - Class, класс в который преобразовывается self.
24
+ #
25
+ # Returns Instance of klass.
26
+ def change_sti_type(klass)
27
+ became = klass.new
28
+ became.instance_variable_set('@attributes', @attributes)
29
+ became.instance_variable_set('@attributes_cache', @attributes_cache)
30
+ became.instance_variable_set('@new_record', new_record?)
31
+ became.instance_variable_set('@destroyed', destroyed?)
32
+ became.type = klass.name unless self.class.descends_from_active_record?
33
+ became
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module StiFriendly
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sti_friendly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sti_friendly'
8
+ spec.version = StiFriendly::VERSION
9
+ spec.authors = ['Stanislav Gordanov']
10
+ spec.email = ['stanislav.gordanov@gmail.com']
11
+ spec.description = %q{Изменение класса у STI модели}
12
+ spec.summary = %q{Изменение класса у STI модели}
13
+ spec.homepage = 'https://github.com/abak-press/sti_friendly'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'activerecord'
22
+ spec.add_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sti_friendly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stanislav Gordanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ version_requirements: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ name: activerecord
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :runtime
26
+ prerelease: false
27
+ - !ruby/object:Gem::Dependency
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: activesupport
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ - !ruby/object:Gem::Dependency
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: '1.3'
53
+ type: :development
54
+ prerelease: false
55
+ - !ruby/object:Gem::Dependency
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: rake
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ description: Изменение класса у STI модели
70
+ email:
71
+ - stanislav.gordanov@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - lib/sti_friendly.rb
81
+ - lib/sti_friendly/version.rb
82
+ - sti_friendly.gemspec
83
+ homepage: https://github.com/abak-press/sti_friendly
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.6
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Изменение класса у STI модели
107
+ test_files: []