1. Generate a facebook_publisher.
    $ ./script/generate facebook_publisher sample
          exists  app/models
          create  app/models/sample_publisher.rb
          exists  db/migrate
          create  db/migrate/20090421171137_create_facebook_templates.rb
  2. Migrate the database. You'll only need to do this once regardless of how many facebook_publishers you create.
    $ rake db:migrate
    (in /Users/philip/Work/PJKH/facebooker_on_rails/web)
    ==  CreateFacebookTemplates: migrating ========================================
    -- create_table(:facebook_templates, {:force=>true})
       -> 0.0016s
    -- add_index(:facebook_templates, :template_name, {:unique=>true})
       -> 0.0806s
    ==  CreateFacebookTemplates: migrated (0.0825s) ===============================
  3. Create the appropriate methods in app/models/sample_publisher.rb.
    class SamplePublisher < Facebooker::Rails::Publisher
    
      def sample_template
        one_line_story_template "{*actor*} spent some of {*hisher*} time on {*weekday*} exploring Facebooker on Rails."
        action_links action_link('Check it out!', 'http://facebooker.pjkh.com')
      end
    
      def sample(user)
        send_as :user_action
        from user.facebook_session.user
        story_size ONE_LINE
        data :hisher => (case user.facebook_session.user.sex when 'male' then 'his' when 'female' then 'her' else 'their' end),
             :weekday => Date.today.strftime('%A')
      end
      
    end
    
  4. Register the template. Be sure to do this both for your development and production environments. Be sure to do this anytime you change the template.
    >> SamplePublisher.register_sample
    facebook.feed.registerTemplateBundle (0.729419946670532) action_links = [{"text": "Check it out!", "href": "http://apps.facebook.com/localhost/"}], one_line_story_templates = ["{*actor*} spent some of {*hisher} time on {*weekday*} exploring Facebooker on Rails."]
      Facebooker::Rails::Publisher::FacebookTemplate Load (0.2ms)   SELECT * FROM "facebook_templates" WHERE ("facebook_templates"."template_name" = 'SamplePublisher::sample') LIMIT 1
      Facebooker::Rails::Publisher::FacebookTemplate Create (0.5ms)   INSERT INTO "facebook_templates" ("bundle_id", "template_name", "content_hash") VALUES(90095065780, 'SamplePublisher::sample', '6b266ca12fbba2b5466906c42155ac39')
    => #
  5. Have the user do something that triggers the action. See Facebook's Feed Policy for acceptable triggers.
  6. For this demonstration we simply POST to this page. The controller action looks like this:
    def user_action
      if request.post?
        @user_action_to_publish = SamplePublisher.create_sample(@current_user)
        end
      end
  7. Then in our applications layout just after the opening body tag we have this. The second to last line is the important one.
    <%= fb_connect_javascript_tag %>
        <% init_fb_connect "XFBML","Api" do %>
          <%= yield :fb_connect %>
          <%= fb_user_action(@user_action_to_publish) if @user_action_to_publish %>
        <% end %>
  8. Click the button below to trigger the action. If you're not using a test account I would recommend clicking 'skip' when the dialog appears. Otherwise it's safe to publish it and you will see it appear in your profile. If this app was much more popular I believe you'd see it appear on your wall directly.

Comments