Class Facebooker::Rails::Publisher::FacebookTemplate
In: lib/facebooker/rails/publisher.rb
Parent: ::ActiveRecord::Base

Methods

Public Class methods

[Source]

# File lib/facebooker/rails/publisher.rb, line 171
          def bundle_id_for_class_and_method(klass,method)
            for_class_and_method(klass,method).bundle_id
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 175
          def cache(klass,method,template)
            template_cache[template_name(klass,method)] = template
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 179
          def clear_cache!
            self.template_cache = {}
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 183
          def find_cached(klass,method)
            template_cache[template_name(klass,method)] || find_in_db(klass,method)
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 187
          def find_in_db(klass,method)
            template = find_by_template_name(template_name(klass,method))
            
            if template.nil? || template.template_changed?(hashed_content(klass, method))
              template = register(klass,method)
            end
            template
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 168
          def for_class_and_method(klass,method)
            find_cached(klass,method) 
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 202
          def hashed_content(klass, method)
            publisher = setup_publisher(klass,method)
            # sort the Hash elements (in the short_story and full_story) before generating MD5
            Digest::MD5.hexdigest [publisher.one_line_story_templates,
               (publisher.short_story_templates and publisher.short_story_templates.collect{|ss| ss.to_a.sort_by{|e| e[0].to_s}}),
               (publisher.full_story_template and publisher.full_story_template.to_a.sort_by{|e| e[0].to_s})
               ].to_json
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 133
        def self.inspect(*args)
          "FacebookTemplate"
        end

[Source]

# File lib/facebooker/rails/publisher.rb, line 156
          def register(klass,method)
            publisher = setup_publisher(klass,method)            
            template_id = Facebooker::Session.create.register_template_bundle(publisher.one_line_story_templates,publisher.short_story_templates,publisher.full_story_template,publisher.action_links)
            template = find_or_initialize_by_template_name(template_name(klass,method))
            template.deactivate if template.bundle_id  # deactivate old templates to avoid exceeding templates/app limit
            template.bundle_id = template_id
            template.content_hash = hashed_content(klass,method) if template.respond_to?(:content_hash)
            template.save!
            cache(klass,method,template)
            template
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 196
          def setup_publisher(klass,method)
            publisher = klass.new
            publisher.send method + '_template'
            publisher
          end

[Source]

# File lib/facebooker/rails/publisher.rb, line 211
          def template_name(klass,method)
            "#{Facebooker.api_key}: #{klass.name}::#{method}"
          end

Public Instance methods

[Source]

# File lib/facebooker/rails/publisher.rb, line 145
        def deactivate
          Facebooker::Session.create.deactivate_template_bundle_by_id(self.bundle_id)
          return true
        rescue Facebooker::Session::TemplateBundleInvalid => e
          return false
        end

[Source]

# File lib/facebooker/rails/publisher.rb, line 137
        def template_changed?(hash)
          if respond_to?(:content_hash)
            content_hash != hash 
          else
            false
          end
        end

[Validate]