has_friendship 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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +24 -0
- data/lib/generators/has_friendship/has_friendship_generator.rb +18 -0
- data/lib/generators/has_friendship/templates/migration.rb +12 -0
- data/lib/has_friendship.rb +14 -0
- data/lib/has_friendship/extender.rb +13 -0
- data/lib/has_friendship/friendable.rb +74 -0
- data/lib/has_friendship/friendship.rb +16 -0
- data/lib/has_friendship/version.rb +3 -0
- data/lib/tasks/has_friendship_tasks.rake +4 -0
- metadata +152 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 09dac4489b7f7828bde6219447dd8298a0912960
         | 
| 4 | 
            +
              data.tar.gz: 17cc29519b2b5a10ca2d56ca157027883ed4834e
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: aae7d2e05e0177b5c04efef5d925b0693a6f92d4735c41f070b8ef1e593aaa4c01a766efe71490c1cfaecf7367279329f0a8e375584f796a2e6df2d1da3396a2
         | 
| 7 | 
            +
              data.tar.gz: 8ba610fe77f4f12dc557afd60a158133f8b4279f22325baf3b1f037941e98b73d52192c2b0f5375a044ad4c9f7649c8823a9fe979c7c7590292379cdcf497683
         | 
    
        data/MIT-LICENSE
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            Copyright 2014 Sung Won Cho
         | 
| 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.
         | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            begin
         | 
| 2 | 
            +
              require 'bundler/setup'
         | 
| 3 | 
            +
            rescue LoadError
         | 
| 4 | 
            +
              puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
         | 
| 5 | 
            +
            end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            require 'rspec/core/rake_task'
         | 
| 8 | 
            +
            require 'rdoc/task'
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            RDoc::Task.new(:rdoc) do |rdoc|
         | 
| 11 | 
            +
              rdoc.rdoc_dir = 'rdoc'
         | 
| 12 | 
            +
              rdoc.title    = 'HasFriendship'
         | 
| 13 | 
            +
              rdoc.options << '--line-numbers'
         | 
| 14 | 
            +
              rdoc.rdoc_files.include('README.rdoc')
         | 
| 15 | 
            +
              rdoc.rdoc_files.include('lib/**/*.rb')
         | 
| 16 | 
            +
            end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            Bundler::GemHelper.install_tasks
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            desc "Run specs"
         | 
| 21 | 
            +
            RSpec::Core::RakeTask.new(:spec)
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            desc "Default: run specs"
         | 
| 24 | 
            +
            task default: :spec
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            require 'rails/generators'
         | 
| 2 | 
            +
            require 'rails/generators/migration'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            class HasFriendshipGenerator < Rails::Generators::Base
         | 
| 5 | 
            +
              include Rails::Generators::Migration
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              def self.source_root
         | 
| 8 | 
            +
                File.join(File.dirname(__FILE__), 'templates')
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              def self.next_migration_number(path)
         | 
| 12 | 
            +
                Time.now.utc.strftime("%Y%m%d%H%M%S")
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              def create_migration_file
         | 
| 16 | 
            +
                migration_template 'migration.rb', 'db/migrate/create_friendships.rb'
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
            end
         | 
| @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            require 'active_support'
         | 
| 2 | 
            +
            require 'active_record'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module HasFriendship
         | 
| 5 | 
            +
              extend ActiveSupport::Autoload
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              autoload :Friendable
         | 
| 8 | 
            +
              autoload :Friendship
         | 
| 9 | 
            +
              autoload :Extender
         | 
| 10 | 
            +
            end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            ActiveSupport.on_load(:active_record) do
         | 
| 13 | 
            +
              extend HasFriendship::Friendable
         | 
| 14 | 
            +
            end
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            module HasFriendship
         | 
| 2 | 
            +
              module Extender
         | 
| 3 | 
            +
                def self.included(base)
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                  # Dynamically define the Friendship's association based on where the module is included.
         | 
| 6 | 
            +
                  HasFriendship::Friendship.class_eval do
         | 
| 7 | 
            +
                    belongs_to :friendable, polymorphic: true
         | 
| 8 | 
            +
                    belongs_to :friend, class_name: "#{base.to_s}", foreign_key: :friend_id
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
            end
         | 
| @@ -0,0 +1,74 @@ | |
| 1 | 
            +
            module HasFriendship
         | 
| 2 | 
            +
              module Friendable
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                def friendable?
         | 
| 5 | 
            +
                  false
         | 
| 6 | 
            +
                end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def has_friendship
         | 
| 9 | 
            +
                  
         | 
| 10 | 
            +
                  class_eval do
         | 
| 11 | 
            +
                    has_many :friendships, as: :friendable, class_name: "HasFriendship::Friendship", dependent: :destroy
         | 
| 12 | 
            +
                    has_many :friends, 
         | 
| 13 | 
            +
                              -> { where friendships: { status: 'accepted' } },
         | 
| 14 | 
            +
                              through: :friendships
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    has_many :requested_friends,
         | 
| 17 | 
            +
                              -> { where friendships: { status: 'requested' } },
         | 
| 18 | 
            +
                              through: :friendships,
         | 
| 19 | 
            +
                              source: :friend
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                    has_many :pending_friends,
         | 
| 22 | 
            +
                              -> { where friendships: { status: 'pending' } },
         | 
| 23 | 
            +
                              through: :friendships,
         | 
| 24 | 
            +
                              source: :friend
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                    def self.friendable?
         | 
| 27 | 
            +
                      true
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                  include HasFriendship::Friendable::InstanceMethods
         | 
| 32 | 
            +
                  include HasFriendship::Extender
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                module InstanceMethods
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  def friend_request(friend)
         | 
| 38 | 
            +
                    unless self == friend || HasFriendship::Friendship.exist?(self, friend)
         | 
| 39 | 
            +
                      transaction do
         | 
| 40 | 
            +
                        HasFriendship::Friendship.create(friendable_id: self.id, friendable_type: self.class.base_class.name, friend_id: friend.id, status: 'pending')
         | 
| 41 | 
            +
                        HasFriendship::Friendship.create(friendable_id: friend.id, friendable_type: friend.class.base_class.name, friend_id: self.id, status: 'requested')
         | 
| 42 | 
            +
                      end
         | 
| 43 | 
            +
                    end
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  def accept_request(friend)
         | 
| 47 | 
            +
                    transaction do
         | 
| 48 | 
            +
                      pending_friendship = HasFriendship::Friendship.find_friendship(friend, self)
         | 
| 49 | 
            +
                      pending_friendship.status = 'accepted'
         | 
| 50 | 
            +
                      pending_friendship.save
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                      requeseted_friendship = HasFriendship::Friendship.find_friendship(self, friend)
         | 
| 53 | 
            +
                      requeseted_friendship.status = 'accepted'
         | 
| 54 | 
            +
                      requeseted_friendship.save
         | 
| 55 | 
            +
                    end
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                  def decline_request(friend)
         | 
| 59 | 
            +
                    transaction do
         | 
| 60 | 
            +
                      HasFriendship::Friendship.find_friendship(friend, self).destroy
         | 
| 61 | 
            +
                      HasFriendship::Friendship.find_friendship(self, friend).destroy
         | 
| 62 | 
            +
                    end
         | 
| 63 | 
            +
                  end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                  def remove_friend(friend)
         | 
| 66 | 
            +
                    transaction do
         | 
| 67 | 
            +
                      HasFriendship::Friendship.find_friendship(friend, self).destroy
         | 
| 68 | 
            +
                      HasFriendship::Friendship.find_friendship(self, friend).destroy
         | 
| 69 | 
            +
                    end
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                end
         | 
| 73 | 
            +
              end
         | 
| 74 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            module HasFriendship
         | 
| 2 | 
            +
              class Friendship < ActiveRecord::Base
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                def self.check_one_side(friendable, friend)
         | 
| 5 | 
            +
                  find_by(friendable_id: friendable.id, friendable_type: friendable.class.base_class.name, friend_id: friend.id).present?
         | 
| 6 | 
            +
                end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def self.exist?(friendable, friend)
         | 
| 9 | 
            +
                  check_one_side(friendable, friend) && check_one_side(friend, friendable)
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def self.find_friendship(friendable, friend)
         | 
| 13 | 
            +
                  HasFriendship::Friendship.find_by(friendable_id: friendable.id, friendable_type: friendable.class.base_class.name, friend_id: friend.id)
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,152 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: has_friendship
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Sung Won Cho
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2014-12-02 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: 4.1.8
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: 4.1.8
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: sqlite3
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rspec-rails
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - ">="
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '0'
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - ">="
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '0'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: guard-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 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: shoulda
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - ">="
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0'
         | 
| 76 | 
            +
              type: :development
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - ">="
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: generator_spec
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - ">="
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - ">="
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 97 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 98 | 
            +
              name: coveralls
         | 
| 99 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
                requirements:
         | 
| 101 | 
            +
                - - ">="
         | 
| 102 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 103 | 
            +
                    version: '0'
         | 
| 104 | 
            +
              type: :development
         | 
| 105 | 
            +
              prerelease: false
         | 
| 106 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - ">="
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0'
         | 
| 111 | 
            +
            description: 
         | 
| 112 | 
            +
            email:
         | 
| 113 | 
            +
            - mikeswcho@gmail.com
         | 
| 114 | 
            +
            executables: []
         | 
| 115 | 
            +
            extensions: []
         | 
| 116 | 
            +
            extra_rdoc_files: []
         | 
| 117 | 
            +
            files:
         | 
| 118 | 
            +
            - MIT-LICENSE
         | 
| 119 | 
            +
            - Rakefile
         | 
| 120 | 
            +
            - lib/generators/has_friendship/has_friendship_generator.rb
         | 
| 121 | 
            +
            - lib/generators/has_friendship/templates/migration.rb
         | 
| 122 | 
            +
            - lib/has_friendship.rb
         | 
| 123 | 
            +
            - lib/has_friendship/extender.rb
         | 
| 124 | 
            +
            - lib/has_friendship/friendable.rb
         | 
| 125 | 
            +
            - lib/has_friendship/friendship.rb
         | 
| 126 | 
            +
            - lib/has_friendship/version.rb
         | 
| 127 | 
            +
            - lib/tasks/has_friendship_tasks.rake
         | 
| 128 | 
            +
            homepage: https://github.com/sungwoncho/has_friendship
         | 
| 129 | 
            +
            licenses:
         | 
| 130 | 
            +
            - MIT
         | 
| 131 | 
            +
            metadata: {}
         | 
| 132 | 
            +
            post_install_message: 
         | 
| 133 | 
            +
            rdoc_options: []
         | 
| 134 | 
            +
            require_paths:
         | 
| 135 | 
            +
            - lib
         | 
| 136 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 137 | 
            +
              requirements:
         | 
| 138 | 
            +
              - - ">="
         | 
| 139 | 
            +
                - !ruby/object:Gem::Version
         | 
| 140 | 
            +
                  version: '0'
         | 
| 141 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 142 | 
            +
              requirements:
         | 
| 143 | 
            +
              - - ">="
         | 
| 144 | 
            +
                - !ruby/object:Gem::Version
         | 
| 145 | 
            +
                  version: '0'
         | 
| 146 | 
            +
            requirements: []
         | 
| 147 | 
            +
            rubyforge_project: 
         | 
| 148 | 
            +
            rubygems_version: 2.4.4
         | 
| 149 | 
            +
            signing_key: 
         | 
| 150 | 
            +
            specification_version: 4
         | 
| 151 | 
            +
            summary: Add social network friendship features to your Active Record models.
         | 
| 152 | 
            +
            test_files: []
         |