Permalink
Browse files

fix migrations

  • Loading branch information...
1 parent 8aa00f5 commit 01bcf80e7b06eb98fc826a96ab402c1fc5bb430f @astevens committed Dec 4, 2012
Showing with 30 additions and 29 deletions.
  1. +30 −29 lib/acts_as_opengraph/active_record/acts/opengraph.rb
@@ -1,51 +1,52 @@
-module ActiveRecord
+module ActiveRecord
module Acts
module Opengraph
-
+
def self.included(base)
base.extend ActMethods
end
-
+
module ActMethods
def acts_as_opengraph(options = {})
# don't allow multiple calls
return if included_modules.include? InstanceMethods
-
+ return unless table_exists?
+
extend ClassMethods
-
+
opengraph_atts = %w(title type image url description site_name latitude longitude street_address locality region postal_code country_name email phone_number fax_number)
-
+
options[:columns] ||= {}
options[:values] ||= {}
-
+
opengraph_atts.each do |att_name|
options[:columns]["#{att_name}".to_sym] ||= alternative_column_name_for("og_#{att_name}".to_sym)
end
-
+
class_attribute :opengraph_atts
self.opengraph_atts = opengraph_atts
-
+
class_attribute :options
self.options = options
-
+
opengraph_atts.each do |att_name|
define_method "opengraph_#{att_name}" do
return_value_or_default att_name.to_sym
end
end
-
+
include InstanceMethods
-
+
end
-
+
end
-
+
module ClassMethods
-
+
private
-
+
# Returns a list of possible column names for a given attribute.
- #
+ #
# @param [Symbol] att_name An opengraph attribute name prefixed with 'og_', i.e. :og_title, :og_type, etc
# @return [Array] A list of possible names for the given opengraph attribute
def alternative_names_for(att_name)
@@ -69,27 +70,27 @@ def alternative_names_for(att_name)
else []
end
end
-
+
# Tries to guess the column name for the given attribute. If it can't find any column (or similar) then it will create a virtual attribute
# for the object called: ATT_NAME_placeholder, so the object responds to that column.
- #
+ #
# @param [Symbol] att_name An opengraph attribute name prefixed with 'og_', i.e. :og_title, :og_type, etc
# @return [String] The final name (found or created) for the opengraph attribute
def alternative_column_name_for(att_name)
alt_names = alternative_names_for(att_name)
columns_to_check = [att_name] + alt_names
- columns_to_check.each do |column_name|
- return column_name.to_sym if column_names.include?(column_name.to_s)
+ columns_to_check.each do |column_name|
+ return column_name.to_sym if table_exists? && column_names.include?(column_name.to_s)
end
-
+
# Define placeholder method
ph_method_name = "#{alt_names.first}_placeholder"
define_method(ph_method_name) { "" }
ph_method_name
end
-
+
end
-
+
module InstanceMethods
# Returns an array of hashes representing the opengraph attribute/values for the Object.
#
@@ -102,20 +103,20 @@ def opengraph_data
end
data_list.delete_if{ |el| el[:value].blank? }
end
-
-
+
+
private
-
+
def return_value_or_default(att_name)
if options[:values].has_key?(att_name.to_sym)
options[:values][att_name]
else
self.send options[:columns]["#{att_name}".to_sym]
end
end
-
+
end
-
+
end
end
end

0 comments on commit 01bcf80

Please sign in to comment.