namespace :db do

  task add_achievements_to_fb: :environment do

  end
end

namespace :db do
  require 'koala'
  require 'json'

  task add_achievements_to_fb: :environment do
    # setup the FB graph
    get_fbgraph_for_app
    app_info = @graph.get_connection("app", "")
    app_namespace = app_info["namespace"]

    #iterate through all undeleted achievements
    Achievement.all.each do |ach|
      badge = init_badge(ach) #prepare the badge object

      #post badge object to FB
      id = @graph.put_connections("app", "objects/#{app_namespace}:badge",
                                  :object => JSON.generate(badge))

      # get ID as response and save it to the db
      ach.facebook_obj_id = id["id"]
      ach.save!
    end
  end

  # same as functions in achievement controller
  def get_fbgraph_for_app
    oauth = Koala::Facebook::OAuth.new
    app_token = oauth.get_app_access_token
    @graph = Koala::Facebook::API.new(app_token)
  end

  def init_badge(ach)
    badge = {"title" => ach.title, "description" => ach.description}

    unless ach.icon_url.blank?
      badge["image"] = ach.icon_url
    end
    badge
  end
end

Complete rake task for adding existing achievements to FB as badges.
Complete rake task for adding existing achievements to FB as badges.

References Coursemology/coursemology.org#192.
