symmetry 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
+ !binary "U0hBMQ==":
3
+ metadata.gz: bc2f0af03b14a6d059fc2ef7ab5085503d93f73a
4
+ data.tar.gz: b7f9423a384a2239bbb38522ed8e46f77b287787
5
+ !binary "U0hBNTEy":
6
+ metadata.gz: 4c732c5f9d47f9eaffcdd13c55c31cc488cf93743cb240dd7968c7fd025cd06d6acf4ac2e5b7c73ea32be69b70ca9e2c26bb4f31a8d8c62fa7a522c892acea9e
7
+ data.tar.gz: b325e1b03eab6d8316c4bc4fd1d5e6ca5ab3294128cf82be2504109dcb749db2d785495c85e1003c7a41b02e315ddaf4e351646e9cd33fd23adebf4a7000fabc
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Daniel Jonasson
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,9 @@
1
+ = Symmetry
2
+
3
+ {<img src="https://travis-ci.org/djonasson/symmetry.png?branch=master" alt="Build Status" />}[https://travis-ci.org/djonasson/symmetry]
4
+ {<img src="https://gemnasium.com/djonasson/symmetry.png" alt="Dependency Status" />}[https://gemnasium.com/djonasson/symmetry]
5
+ {<img src="https://codeclimate.com/github/djonasson/symmetry.png" alt="Code Climate" />}[https://codeclimate.com/github/djonasson/symmetry]
6
+ {<img src="https://coveralls.io/repos/djonasson/symmetry/badge.png?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/djonasson/symmetry]
7
+ {<img src="https://badge.fury.io/rb/symmetry.png" alt="Gem Version" />}[http://badge.fury.io/rb/symmetry]
8
+
9
+ Symmetry gives you a simple way of creating symmetric has_and_belongs_to_many relationships (friendships, neighbors, etc.) in your Active Record models.
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Symmetry'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+
26
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
27
+ load 'rails/tasks/engine.rake'
28
+
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec)
31
+ task default: :spec
@@ -0,0 +1,28 @@
1
+ class SymmetricRelationship < ActiveRecord::Base
2
+ attr_accessible :owner_id, :owner_type, :relation_id, :relation_type
3
+
4
+ belongs_to :owner, polymorphic: true
5
+ belongs_to :relation, polymorphic: true
6
+
7
+ validates :owner_id, presence: true, uniqueness: { scope: [:owner_type, :relation_id, :relation_type] }
8
+ validates :owner_type, presence: true
9
+ validates :relation_id, presence: true
10
+ validates :relation_type, presence: true
11
+
12
+ after_create :create_symetric_relation
13
+ after_destroy :destroy_symetric_relation
14
+
15
+ private
16
+
17
+ def create_symetric_relation
18
+ if self.class.where(owner_id: relation_id, owner_type: relation_type, relation_id: owner_id, relation_type: owner_type).empty?
19
+ self.class.create(owner_id: relation_id, owner_type: relation_type, relation_id: owner_id, relation_type: owner_type)
20
+ end
21
+ end
22
+
23
+ def destroy_symetric_relation
24
+ self.class.where(owner_id: relation_id, owner_type: relation_type, relation_id: owner_id, relation_type: owner_type).each do |record|
25
+ record.delete
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,2 @@
1
+ Symmetry::Engine.routes.draw do
2
+ end
@@ -0,0 +1,11 @@
1
+ class CreateSymmetricRelationships < ActiveRecord::Migration
2
+ def change
3
+ create_table :symmetric_relationships do |t|
4
+ t.integer :owner_id
5
+ t.string :owner_type
6
+ t.integer :relation_id
7
+ t.string :relation_type
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ require "symmetry/engine"
2
+ require "symmetry/active_record"
3
+
4
+ module Symmetry
5
+ end
6
+
7
+ ActiveRecord::Base.extend(Symmetry::ActiveRecord)
@@ -0,0 +1,16 @@
1
+ module Symmetry
2
+ module ActiveRecord
3
+
4
+ def symmetric_relation(relation_name, options = {})
5
+ options.assert_valid_keys(:polymorphic_relation_name)
6
+
7
+ relation_name_singular = relation_name.to_s.singularize
8
+ polymorphic_relation_name = options[:polymorphic_relation_name].presence || "#{relation_name_singular}_relations"
9
+
10
+ attr_accessible "#{relation_name_singular}_ids"
11
+ has_many polymorphic_relation_name, class_name: "SymmetricRelationship", as: :owner, dependent: :destroy
12
+ has_many relation_name, through: polymorphic_relation_name, as: :relation, source: :relation, source_type: self.name
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Symmetry
2
+ class Engine < ::Rails::Engine
3
+ #isolate_namespace Symmetry
4
+ config.generators do |g|
5
+ #g.template_engine :haml
6
+ g.integration_tool :rspec
7
+ g.test_framework :rspec, view_specs: false
8
+ #g.fixture_replacement :factory_girl, dir: 'spec/factories'
9
+ g.orm :active_record
10
+ #g.stylesheets true
11
+ #g.form_builder :simple_form
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Symmetry
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+ desc "Setup test db and run tests"
3
+ task test: :environment do
4
+ Rake::Task['app:db:migrate'].execute
5
+ Rake::Task['app:db:test:prepare'].execute
6
+ Rake::Task['spec'].execute
7
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: symmetry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Jonasson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ description: |-
28
+ Symmetry gives you a simple way of creating symmetric
29
+ has_and_belongs_to_many relationships (friendships, neighbors, etc.) in
30
+ your Active Record models. More information can be found at:
31
+ https://github.com/djonasson/worldwise
32
+ email:
33
+ - daniel@guadeo.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - app/models/symmetric_relationship.rb
39
+ - config/routes.rb
40
+ - db/migrate/20130201161328_create_symmetric_relationships.rb
41
+ - lib/tasks/testing_tasks.rake
42
+ - lib/symmetry/active_record.rb
43
+ - lib/symmetry/engine.rb
44
+ - lib/symmetry/version.rb
45
+ - lib/symmetry.rb
46
+ - MIT-LICENSE
47
+ - Rakefile
48
+ - README.rdoc
49
+ homepage: https://github.com/djonasson/symmetry
50
+ licenses: []
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.0.0.preview3.1
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Symmetry gives you a simple way of creating symmetric has_and_belongs_to_many
72
+ relationships (friendships, neighbors, etc.) in your Active Record models.
73
+ test_files: []