Skip to content

How to add new activity objects

holacarol edited this page Aug 22, 2013 · 8 revisions

Social Stream allows developers to write and integrate custom models in the social network.

Activity objects already available

Social Stream components already provide your application with the most popular activity objects (for instance social_stream-documents provides with files, pictures, audio and video support) These gems automatically integrate new activity objects with the rest of Social Stream, in the wall form, the search engine, etc.

Just include the desired components in your Gemfile.

Besides, some aspects can be customized in the file config/initializers/social_stream.rb, which should be copied to your application in the install rake task, and should look like https://github.com/ging/social_stream/blob/master/base/lib/generators/social_stream/base/templates/initializer.rb

Creating new activity objects

For adding new features on social_stream, remember:

  • For every new activity object:
    • Start by creating your new activity model as any other resource in rails. For instance, rails generate scaffold MyObject first_specific_attribute:integer second_specific_attribute:string
    • The models should include SocialStream::Models::Object, under app/models/my_object.rb
    • The controller for this model, if exists, should include SocialStream::Controllers::Objects, under app/controllers/my_objects_controller.rb
    • The model should include an activity_object_id integer field in their table. Therefore, the corresponding migration should be modified, under app/db/migrations/ (before running rake db:migrate, of course):
    create_table :my_objects do |t|
      t.references :activity_object ## equivalent to t.integer :activity_object_id
      ...
      t.timestamps
    end
  • Add the model name to config/initializers/social_stream.rb, at least in the config.objects section.
  • Remove the line in config/routes.rb created for your model (e.g.resources :myobjects). Social Stream created the routes for you with the previous step.
  • The specific attributes of your model need to be added to the allowed_params method in your controller, in order to create and update them correctly. Like this:
class MyObjectsController < ApplicationController
  include SocialStream::Controllers::Objects
  ...
  def allowed_params
    [:first_specific_attribute, :second_specific_attribute]
  end
  ...
end
  • Your objects will be now correctly created and updated, not needing a create or update method. However, if something else needs to be done at creation/update time, they can be defined, but you need to:

    • Assign the owner/author/user_author ids
    • Create/update the model with permitted_params instead of params['my_object']. This will give permission to these parameters to be used in the model creation, as is described in the strong parameters gem (built-in in rails 4)
    def create
      ...
      params['my_object']['owner_id'] = current_subject.try(:actor_id)
      params['my_object']['author_id'] = current_subject.try(:actor_id)
      params['my_object']['user_author_id'] = current_user.try(:actor_id)
      my_object = MyObject.new(permitted_params)
      if my_object.save
        ... # success action
      else
        ... # error action
      end
      ...
    end
    def update
      ...
      my_object = MyObject.find(params[:id])
      ...
      if my_object.update_attributes!(permitted_params)
        ... # success action
      else
        ... # error action
      end
    end
  • If you want to create new object from the wall input, you must add the object to the config.activity_forms section in config/initializers/social_stream.rb, and create a view in app/views/my_models/_new_activity.html.erb. Take a look at linkser example at https://github.com/ging/social_stream/blob/master/linkser/app/views/links/_new_activity.html.erb. If you don't want to use the wall input to create your object, then you should not add your object to this section.

  • Once you create the activity object, it is necessary that you write some views for showing new objects in timeline, search engine, repository etc...

    • Firstly, you must include it in config/initializers/social_stream.rb so they are included in the views. See your application config/initializers/social_stream.rb for more details.
    • Finally, you must create the appropiate views. You can use the views in the linkser gem as a reference for building your own views: https://github.com/ging/social_stream/tree/master/linkser/app/views/links Current views are:
      • app/views/my_objects/_timeline.html.erb shows the object in the activities timeline
      • app/views/my_objects/_my_object.html.erb the object in the repository
      • app/views/my_objects/_quick_search_result.html.erb the object in the search hints from the header
      • app/views/my_objects/_search_result.html.erb the object in the full search result
Clone this wiki locally