fake_associations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b0072e97c5fc932d700caff1aba3568b62215b2
4
+ data.tar.gz: f3528c7cdbc5fa44146e2f170ca82e84871598a7
5
+ SHA512:
6
+ metadata.gz: 820a0ab7a1c49c7b36e1e828b3ce9746ab7cb736e30a02d7da54178b03935ecf0b7db6cf647723566ddb99dd59778542ad29cf4827b82db74d56bbddc03aaed2
7
+ data.tar.gz: 2d7ad0cbe1d3126870c31572dfd90d5251cd85bf74f990be35456a133cf50040203cddf1aedd6587501b7787d6de46e227881e7b2d72648998214a4ef8113f09
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fake_association.gemspec
4
+ gemspec
@@ -0,0 +1,82 @@
1
+ # FakeAssociations
2
+
3
+ To enable to use ActiveRecord association in the module is not ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fake_associations'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fake_associations
20
+
21
+ ## Usage
22
+
23
+ It has been confirmed that the following code to work
24
+
25
+ ```ruby
26
+ class User
27
+ include FakeAssociations
28
+
29
+ # Need
30
+ attr_accessor :id
31
+
32
+ has_many :tweets
33
+
34
+ has_many :friendships, foreign_key: 'follower_id'
35
+ has_many :followed_user, through: :friendships, source: :followed
36
+
37
+ has_many :reverse_friendships, foreign_key: 'followed_id', class_name: 'Friendship'
38
+ has_many :followers, through: :reverse_friendships, source: :follower
39
+
40
+ has_many :favorite_tweets, through: :favorites, source: :tweet
41
+ end
42
+
43
+ class Friendship < ActiveRecord::Base
44
+ # Table name: friendships
45
+ #
46
+ # id :integer not null, primary key
47
+ # follower_id :integer
48
+ # followed_id :integer
49
+ end
50
+
51
+ class Tweet < ActiveRecord::Base
52
+ # Table name: tweets
53
+ #
54
+ # id :integer not null, primary key
55
+ # user_id :integer
56
+ end
57
+
58
+ class Favorite < ActiveRecord::Base
59
+ # Table name: favorites
60
+ #
61
+ # id :integer not null, primary key
62
+ # user_id :integer
63
+ # tweet_id :integer
64
+ end
65
+ ```
66
+
67
+ ## Limitation
68
+
69
+ Support associations is only `has_many` and limited options.
70
+
71
+ In the future, will support more association.
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
+
77
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fake_associations.
82
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fake_associations"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -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 'fake_associations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fake_associations"
8
+ spec.version = FakeAssociations::VERSION
9
+ spec.authors = ["Tomohiro Suwa"]
10
+ spec.email = ["neoen.gsn@gmail.com"]
11
+
12
+ spec.summary = %q{To enable to use ActiveRecord association in the module is not ActiveRecord.}
13
+ spec.description = %q{To enable to use ActiveRecord association in the module is not ActiveRecord.}
14
+ spec.homepage = "https://github.com/tsuwatch/fake_associations"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,56 @@
1
+ require "fake_associations/version"
2
+
3
+ module FakeAssociations
4
+ extend ActiveSupport::Concern
5
+ DEFINED_METHODS = {}
6
+
7
+ module ClassMethods
8
+ def has_many(target_name, options = {})
9
+ DEFINED_METHODS[target_name] = { options: options }
10
+
11
+ base_class_name = self.name
12
+ target_class_name = target_name.to_s.chomp.classify
13
+ target_column_name = options[:foreign_key] || "#{self.name.downcase}_id"
14
+
15
+ if options[:through].present?
16
+ through_class_name = options[:through].to_s.classify
17
+ target_column_name = :id
18
+
19
+ select_through_column_name = :id
20
+
21
+ if options[:source].present?
22
+ target_class_name = options[:source].to_s.classify
23
+ select_through_column_name = "#{options[:source]}_id".to_sym
24
+
25
+ through_class_name = DEFINED_METHODS[
26
+ options[:through]][:options][:class_name] || through_class_name
27
+
28
+ through_reflection =
29
+ through_class_name.constantize.reflect_on_association(
30
+ options[:source])
31
+ target_class_name = through_reflection.options[:class_name] if through_reflection.options[:class_name].present?
32
+ end
33
+ end
34
+
35
+ target_class_name = options[:class_name] if options[:class_name]
36
+
37
+ define_method(target_name) do
38
+ if options[:through].present?
39
+ through_records = send(options[:through])
40
+ targets = through_records.select(select_through_column_name)
41
+ end
42
+
43
+ targets ||= [self.id]
44
+ if target_class_name == base_class_name
45
+ targets.map do |target|
46
+ id = target.send(select_through_column_name)
47
+ base_class_name.constantize.new(id: id)
48
+ end
49
+ else
50
+ target_class_name.constantize.where(
51
+ "#{target_column_name}": targets)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module FakeAssociations
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fake_associations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomohiro Suwa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: To enable to use ActiveRecord association in the module is not ActiveRecord.
70
+ email:
71
+ - neoen.gsn@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - fake_associations.gemspec
84
+ - lib/fake_associations.rb
85
+ - lib/fake_associations/version.rb
86
+ homepage: https://github.com/tsuwatch/fake_associations
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.4.5
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: To enable to use ActiveRecord association in the module is not ActiveRecord.
109
+ test_files: []