activerecord-devkit 1.0.0

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.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Sergey Tokarenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ Activerecord Devkit
2
+ ===================
3
+ [![Version](https://badge.fury.io/rb/activerecord-devkit.svg)](http://badge.fury.io/rb/activerecord-devkit)
4
+ [![Build](https://travis-ci.org/stokarenko/activerecord-devkit.svg?branch=master)](https://travis-ci.org/stokarenko/activerecord-devkit)
5
+ [![Climate](https://codeclimate.com/github/stokarenko/activerecord-devkit/badges/gpa.svg)](https://codeclimate.com/github/stokarenko/activerecord-devkit)
6
+ [![Coverage](https://codeclimate.com/github/stokarenko/activerecord-devkit/badges/coverage.svg)](https://codeclimate.com/github/stokarenko/activerecord-devkit/coverage)
7
+
8
+ ActiveRecord DevKit is a package of utility Ruby Features for ActiveRecord.
9
+
10
+ ## requirements
11
+ * Ruby >= 1.9.3
12
+ * [ruby-features](https://github.com/stokarenko/ruby-features) >= 1.1.1
13
+ * ActiveRecord >= 3.2.13
14
+
15
+ ## Getting started
16
+ Add to your Gemfile:
17
+
18
+ ```ruby
19
+ gem 'activerecord-devkit'
20
+ ```
21
+
22
+ Run the bundle command to install it.
23
+
24
+ For Rails projects, gun generator:
25
+ ```console
26
+ rails generate ruby_features:install
27
+ ```
28
+
29
+ and apply required features in `config/initializers/ruby-features.rb`:
30
+ ```ruby
31
+ RubyFeatures.apply('activerecord_devkit/association_soft_build')
32
+ ```
33
+
34
+ For no-Rails usage, apply required features just like:
35
+ ```ruby
36
+ require 'activerecord-devkit'
37
+
38
+ RubyFeatures.apply('activerecord_devkit/association_soft_build')
39
+ ```
40
+
41
+ ## Features
42
+ ### activerecord_devkit/association_soft_build
43
+ Makes possible to build associated object without affecting to parent object's association scope:
44
+ ```ruby
45
+ user = User.first
46
+
47
+ ## Regular build, user.roles.size going to be incremented
48
+ new_role = user.roles.build
49
+
50
+ ## Soft build, user.roles.size will stay as is
51
+ new_role = user.roles.soft_build
52
+ # or
53
+ new_role = user.association(:roles).soft_build
54
+ ```
55
+
56
+ ## License
57
+ MIT License. Copyright (c) 2015 Sergey Tokarenko
@@ -0,0 +1,5 @@
1
+ require 'activerecord-devkit/version'
2
+
3
+ require 'ruby-features'
4
+
5
+ RubyFeatures.find_in_path(File.expand_path('../activerecord-devkit/features', __FILE__))
@@ -0,0 +1,29 @@
1
+ RubyFeatures.define 'activerecord_devkit/association_soft_build' do
2
+ condition(:activerecord_3){ ActiveRecord::VERSION::MAJOR == 3 }
3
+
4
+ apply_to 'ActiveRecord::Associations::CollectionProxy' do
5
+ applied do
6
+ delegate :soft_build, to: :@association
7
+ end
8
+ end
9
+
10
+ apply_to 'ActiveRecord::Associations::CollectionAssociation' do
11
+ instance_methods if: :activerecord_3 do
12
+ def soft_build(attributes = {}, options = {}, &block)
13
+ build_record(attributes, options, &block).tap do |record|
14
+ yield(record) if block_given?
15
+ set_inverse_instance record
16
+ end
17
+ end
18
+ end
19
+
20
+ instance_methods unless: :activerecord_3 do
21
+ def soft_build(attributes = {}, &block)
22
+ build_record(attributes, &block).tap do |record|
23
+ yield(record) if block_given?
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordDevkit
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,35 @@
1
+ describe 'association_soft_build' do
2
+ before(:all) { RubyFeatures.apply 'activerecord_devkit/association_soft_build' }
3
+
4
+ subject { User.create! }
5
+
6
+ it 'should set user`s id after creation' do
7
+ expect(subject.id).to be_kind_of(Integer)
8
+ end
9
+
10
+ it 'should has emplty associations set' do
11
+ expect(subject.roles).to be_empty
12
+ end
13
+
14
+ it 'should build associations as usually' do
15
+ role = subject.roles.build
16
+ expect(role.user_id).to eq(subject.id)
17
+ expect(role.user.object_id).to eq(subject.object_id)
18
+ expect(subject.roles).to_not be_empty
19
+ end
20
+
21
+ it 'should build associations without affecting to parent object' do
22
+ role = subject.association(:roles).soft_build
23
+ expect(role.user_id).to eq(subject.id)
24
+ expect(role.user.object_id).to eq(subject.object_id)
25
+ expect(subject.roles).to be_empty
26
+ end
27
+
28
+ it 'should build associations by relation without affecting to parent object' do
29
+ role = subject.roles.soft_build
30
+ expect(role.user_id).to eq(subject.id)
31
+ expect(role.user.object_id).to eq(subject.object_id)
32
+ expect(subject.roles).to be_empty
33
+ end
34
+
35
+ end
@@ -0,0 +1,9 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'bundler/setup'
5
+ Bundler.require(:default)
6
+
7
+ require 'active_record'
8
+
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,24 @@
1
+ RSpec.configure do |config|
2
+ config.before(:all) do
3
+ create_database
4
+ end
5
+
6
+ def create_database
7
+ ActiveRecord::Migration.verbose = false
8
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
9
+ ActiveRecord::Schema.define(version: 1) do
10
+ create_table :users
11
+ create_table :roles do |t|
12
+ t.references :user
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class User < ActiveRecord::Base
19
+ has_many :roles, inverse_of: :user
20
+ end
21
+
22
+ class Role < ActiveRecord::Base
23
+ belongs_to :user
24
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-devkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergey Tokarenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-features
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.1
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: 1.1.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.13
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: 3.2.13
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
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: Collection of utility Ruby Features for ActiveRecord.
63
+ email: private.tokarenko.sergey@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - lib/activerecord-devkit/features/association_soft_build_feature.rb
69
+ - lib/activerecord-devkit/version.rb
70
+ - lib/activerecord-devkit.rb
71
+ - LICENSE
72
+ - README.md
73
+ - spec/association_soft_build_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/support/database.rb
76
+ homepage: https://github.com/stokarenko/activerecord-devkit
77
+ licenses:
78
+ - MIT
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: 1.9.3
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.23.2
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Collection of utility Ruby Features for ActiveRecord.
101
+ test_files:
102
+ - spec/association_soft_build_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/support/database.rb