Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Socialization gem setup [closed]

I went through the documentation of the socialization gem and it does not explain thoroughly how to setup the gem up in my routes and controller to have the follow and mention features function properly.

I was wondering if anyone could show me how to set up this gem in my routes and controller to have it functioning properly.

A thoughtful answer would be greatly appreciated.

like image 338
user3267981 Avatar asked Feb 04 '14 00:02

user3267981


2 Answers

I'm the author of Socialization. Here's some code taken from our application. We have a SocializationsController that handles like & follow for every model. It's pretty straightforward.

# routes.rb
## snip ##
resources :users do
  post 'follow',   to: 'socializations#follow'
  post 'unfollow', to: 'socializations#unfollow'
end

resources :categories, only: [:index] do
  post 'follow',   to: 'socializations#follow'
  post 'unfollow', to: 'socializations#unfollow'
end
## snip ##

# socializations_controller.rb
class SocializationsController < ApplicationController
  before_filter :load_socializable

  def follow
    current_user.follow!(@socializable)
    render json: { follow: true }
  end

  def unfollow
    current_user.unfollow!(@socializable)
    render json: { follow: false }
  end

private
  def load_socializable
    @socializable =
      case
      when id = params[:comment_id] # Must be before :item_id, since it's nested under it.
        @community.comments.find(id)
      when id = params[:item_id]
        @community.items.find(id)
      when id = params[:user_id]
        User.find(id)
      when id = params[:category_id]
        @community.categories.find_by_id(id)
      else
        raise ArgumentError, "Unsupported socializable model, params: " +
                             params.keys.inspect
      end
    raise ActiveRecord::RecordNotFound unless @socializable
  end  
end

For mentions, you just have to parse a comment where a mention is present, for example, and manually create the mention with code. It should be fairly straightforward.

like image 113
Carl Mercier Avatar answered Oct 17 '22 13:10

Carl Mercier


Edit: Check out Carl's solution, definitely much DRYer!

Look through the documentation, you should be able to implement it in your controller and routes in whatever way you want to, all the gem is doing is creating tables in your database for follows and mentions and associated them in your models. But one simple way you could do it with follows (a user can follow another user) is:

In your config/routes.rb

YourApp::Application.routes.draw do
  resources :users do
    member do
      post :follow
    end
  end
end

And that should give you a route of /users/:id/follow and the follow_users_path

In your app/controllers/users_controller.rb

class UsersController < ApplicationController
  def follow
    user = User.find(params[:id])
    current_user.follow!(user) # => This assumes you have a variable current_user who is authenticated
  end
end

And this assumes in your app/models/user.rb, you have

class User < ActiveRecord::Base
  acts_as_follower
  acts_as_followable
end

And in your view, you can have the method

link_to('Follow', follow_user_path(user), method: :post) # => Assumes user is the user you want to follow

So if you click that link, it should take you to the follow action in the users controller and allow the current user to follow the user

Please let me know if there are any errors or typos I might have missed. I'm not sure if this is what you are looking for but I hope this helps.

like image 27
strivedi183 Avatar answered Oct 17 '22 13:10

strivedi183