bfriend 0.7.3

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: 622e58967a1a4efcec7f98f6e09d07b32870dca5
4
+ data.tar.gz: 7bf237f63837034e035fa657b49a8e1e55fe0519
5
+ SHA512:
6
+ metadata.gz: 62ad98686710315b30cc7b1ba61519545cafc919251145af521e1e512b53d536a9e3cc680446671dc36f1ae9d4cae2240c868c69de50153b925972211bdeb7fb
7
+ data.tar.gz: 1be1b815d8930aff9056de13e4eb0f5e41d7c451aa692b2673a73400bedb5c5c23c403f077bb0453393dac7335d870bd244125986b1dc490d283c80a32de6465
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/XFinger/bfriend" }
4
+
5
+ # Specify your gem's dependencies in bfriend.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 xfinger
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+
2
+
3
+ I often need friendship modeling for projects so I created a generator to get up and running quickly.
4
+ This is a simple generator for creating a Self-Referential Association for a friendship model. Heavily based on [Ryan Bates rails cast #163](http://railscasts.com/episodes/163-self-referential-association) with some help from [this guy on github](https://github.com/tobyond?tab=repositories). It isn't particularly sophisticated or polished and doesn't include any tests so use at your own risk.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'bfriend'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ gem install bfriend
21
+
22
+ ## Usage
23
+
24
+ Run the bfriend generator `rails g bfriend -t --api`. This will generate a friendship model, a migration, add routes & associations to your user model and generate a friendship controller suitable for use in an API.
25
+
26
+ If you are using the gem for a ruby on rails project, run `rails g bfriend -t --ror` .
27
+
28
+ Edit the migration to give the status a default of false
29
+
30
+ t.boolean :accepted, default: false"
31
+
32
+ Run your migration
33
+
34
+ Now you can:
35
+
36
+ request friendship: Friendship.create( user_id: id, friend_id: id )
37
+ accept friendship: Friendship.update( status: true )
38
+ decline friendship: Friendship.delete
39
+ end friendship: Friendship.delete
40
+ all friends: user.friends
41
+ all requests: user.pending
42
+ friend_requests: user.pending_friends
43
+ requested_friends: user.requested_friends
44
+ friendship by user: user.current_friends
45
+ friendship by friend: user.bfriended_friends
46
+
47
+
48
+ At this time no view templates are created and I will update this readme when and if I add them.
49
+
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/XFinger/bfriend.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bfriend/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bfriend"
8
+ spec.version = Bfriend::VERSION
9
+ spec.authors = ["xfinger"]
10
+ spec.email = ["xfinger@gmail.com"]
11
+
12
+ spec.summary = %q{A simple friendship model}
13
+ spec.description = %q{Based on Railscasts #163 Self-Referential Association}
14
+ spec.homepage = "https://github.com/XFinger/bfriend"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bfriend"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require "bfriend/version"
2
+
3
+ module Bfriend
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ require "bfriend/version"
2
+
3
+ module Bfriend
4
+ VERSION = "0.7.3"
5
+ end
@@ -0,0 +1,69 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record'
4
+
5
+ class BfriendGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ include Rails::Generators::Migration
8
+ class_option :controller_template, :type => :string, :aliases => "-t", :desc => "Template engine for the Controller. Available options are 'api' and 'ror'.", :default => "api"
9
+ class_option :ror, :type => :boolean, :default => false
10
+
11
+
12
+ # Implement the required interface for Rails::Generators::Migration
13
+ def self.next_migration_number(dirname)
14
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
15
+ end
16
+
17
+ source_root File.expand_path('../templates', __FILE__)
18
+
19
+ def add_route
20
+ route "resources :friendships, only: [:create, :update, :destroy]"
21
+ end
22
+
23
+
24
+ def add_to_user
25
+ inject_into_file 'app/models/user.rb', before: "end" do
26
+ "
27
+ has_many :friendships
28
+ has_many :bfriended_friendships, :class_name => \"Friendship\", :foreign_key => \"friend_id\"
29
+
30
+ # bfriended by user
31
+ has_many :current_friends, -> { where(friendships: { status: true}) }, through: :friendships, source: :friend
32
+ # bfriended by friend
33
+ has_many :bfriended_friends, -> { where(friendships: { status: true}) }, through: :bfriended_friendships, source: :user
34
+ # requested by friend
35
+ has_many :requested_friends, -> { where(friendships: { status: false}) }, through: :bfriended_friendships, source: :user
36
+ # requested by user
37
+ has_many :pending_friends, -> { where(friendships: { status: false}) }, through: :friendships, source: :friend
38
+ \n
39
+
40
+ # combine the sets to see all your friends
41
+ def friends
42
+ current_friends | bfriended_friends
43
+ end
44
+
45
+ # combine the sets to see pending and requested friendships
46
+ def pending
47
+ pending_friends | requested_friends
48
+ end
49
+
50
+ \n
51
+ "
52
+
53
+ end
54
+ end
55
+
56
+ def copy_templates
57
+ template "model.rb", "app/models/friendship.rb"
58
+ if options[:controller_template].to_s == "ror" or options[:ror]
59
+ template "ror_controller.rb", "app/controllers/friendships_controller.rb"
60
+ else
61
+ template "api_controller.rb", "app/controllers/friendships_controller.rb"
62
+ end
63
+ end
64
+
65
+ def generate_migration #after generating the migration, add 'default: false' to status
66
+ generate "migration", "create_friendships user_id:integer friend_id:integer status:boolean "
67
+ end
68
+
69
+ end
@@ -0,0 +1,46 @@
1
+ class FriendshipsController < ApplicationController
2
+ before_action :set_friendship, only: [:destroy]
3
+
4
+ def create
5
+ @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
6
+ if @bfriend.save
7
+ render json: @friend, status: :created, location: @friend
8
+ else
9
+ render json: @friend.errors, status: :unprocessable_entity
10
+ end
11
+ end
12
+
13
+ def destroy
14
+ @friendship.destroy
15
+ end
16
+
17
+ def update
18
+ @friendship = Friendship.find_by(id: params[:id])
19
+ @friendship.update(status: "true")
20
+ if @friendship.save
21
+ render json: @friend, status: :updated, location: @friend
22
+ else
23
+ render json: @friend.errors, status: :unprocessable_entity
24
+ end
25
+ end
26
+
27
+
28
+ def destroy
29
+ @friendship = current_user.friendships.find(params[:id])
30
+ @friendship.destroy
31
+ end
32
+
33
+ private
34
+ # Use callbacks to share common setup or constraints between actions.
35
+ def set_friendship
36
+ @friend = friend.find(params[:id])
37
+ end
38
+
39
+ # Only allow a trusted parameter "white list" through.
40
+ def friend_params
41
+ params.require(:friend).permit(:user_id)
42
+ end
43
+ end
44
+
45
+
46
+
@@ -0,0 +1,8 @@
1
+
2
+ class Friendship < ApplicationRecord
3
+
4
+ belongs_to :user
5
+ belongs_to :friend, :class_name => 'User'
6
+
7
+
8
+ end
@@ -0,0 +1,48 @@
1
+ class FriendshipsController < ApplicationController
2
+ before_action :set_friendship, only: [:destroy]
3
+
4
+ def create
5
+ @friendship = current_user.friendships.build(friend_id: params[:friend_id])
6
+ if @friendship.save
7
+ flash[:notice] = "Friend requested."
8
+ redirect_to :back
9
+ else
10
+ flash[:error] = "Friend request didn't work"
11
+ redirect_to :back
12
+ end
13
+ end
14
+
15
+ def update
16
+ @friendship = Friendship.find_by(id: params[:id])
17
+ @friendship.update(status: "true")
18
+ if @friendship.save
19
+ redirect_to root_url, notice: "Friendship confirmed"
20
+ else
21
+ redirect_to root_url, notice: "Sorry! Could not confirm friendship!"
22
+ end
23
+ end
24
+
25
+ def destroy
26
+ @friendship = Friendship.find_by(id: params[:id])
27
+ @friendship.destroy
28
+ flash[:notice] = "Ended friendship."
29
+ redirect_to :back
30
+ end
31
+
32
+ private
33
+ # Use callbacks to share common setup or constraints between actions.
34
+ def set_friendship
35
+ @friend = friend.find(params[:id])
36
+ end
37
+
38
+ # Only allow a trusted parameter "white list" through.
39
+ def friend_params
40
+ params.require(:friend).permit(:user_id)
41
+ end
42
+
43
+ end
44
+
45
+
46
+
47
+
48
+
@@ -0,0 +1,15 @@
1
+ has_many :friendships
2
+ has_many :received_friendships, class_name: "Friendship", foreign_key: "friend_id"
3
+
4
+ has_many :active_friends, -> { where(friendships: { accepted: true}) }, through: :friendships, source: :friend
5
+ has_many :received_friends, -> { where(friendships: { accepted: true}) }, through: :received_friendships, source: :user
6
+ has_many :pending_friends, -> { where(friendships: { accepted: false}) }, through: :friendships, source: :friend
7
+ has_many :requested_friendships, -> { where(friendships: { accepted: false}) }, through: :received_friendships, source: :user
8
+
9
+ def friends
10
+ active_friends | received_friends
11
+ end
12
+
13
+ def pending
14
+ pending_friends | requested_friendships
15
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bfriend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.3
5
+ platform: ruby
6
+ authors:
7
+ - xfinger
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: 'Based on Railscasts #163 Self-Referential Association'
42
+ email:
43
+ - xfinger@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bfriend.gemspec
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/bfriend.rb
57
+ - lib/bfriend/version.rb
58
+ - lib/generators/bfriend/bfriend_generator.rb
59
+ - lib/generators/bfriend/templates/api_controller.rb
60
+ - lib/generators/bfriend/templates/model.rb
61
+ - lib/generators/bfriend/templates/ror_controller.rb
62
+ - lib/generators/bfriend/tmp.rb
63
+ homepage: https://github.com/XFinger/bfriend
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ allowed_push_host: https://rubygems.org
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.6.4
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A simple friendship model
88
+ test_files: []