Created
June 9, 2010 18:37
-
-
Save logandk/431946 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.tooltip = function(options) { | |
var element = $(this); | |
settings = jQuery.extend({ | |
message: element.attr("alt"), | |
hover: true, | |
insertion: 'body' | |
}, options); | |
// Ensure that a message has been set | |
if (!settings.message.length) | |
return false; | |
// Build tooltip | |
var tooltip = $('<div class="tooltip" />'); | |
tooltip.css('display', 'none'); | |
tooltip.text(settings.message); | |
$(settings.insertion).append(tooltip); | |
// Attach events | |
element.bind('tooltip:show', function () { | |
tooltip.stop(true, true); | |
if (tooltip.is(':visible')) | |
return false; | |
var position = element.offset(); | |
var top = position.top - tooltip.height(); | |
var left = position.left + (element.width() / 2) - (tooltip.width() / 2); | |
tooltip.css({ | |
top: (top - 10) + 'px', | |
left: left + 'px', | |
height: tooltip.height(), | |
width: tooltip.width(), | |
opacity: 0, | |
display: 'block', | |
position: 'absolute' | |
}); | |
tooltip.animate({ top: top + 'px', opacity: 1 }); | |
}); | |
element.bind('tooltip:hide', function () { | |
tooltip.delay(1000).fadeOut(); | |
}); | |
if (settings.hover) { | |
element.hover( | |
function () { | |
element.trigger('tooltip:show'); | |
}, | |
function () { | |
element.trigger('tooltip:hide'); | |
}); | |
} | |
return element; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GoodDefaults | |
module InstanceMethods | |
def initialize_with_defaults(attributes = nil) | |
initialize_without_defaults(attributes) do | |
if defaults_enabled | |
stringified_attributes = attributes.stringify_keys rescue {} | |
self.class.defaults.each do |default_attr, default_value| | |
next if stringified_attributes.has_key?(default_attr) | |
send("#{default_attr}=", default_value) unless self.class.reflect_on_association(default_attr.to_sym) | |
end | |
end | |
yield(self) if block_given? | |
end | |
end | |
end | |
module ClassMethods | |
def has_good_defaults | |
include InstanceMethods | |
alias_method_chain :initialize, :defaults | |
class_inheritable_reader :defaults_enabled | |
write_inheritable_attribute :defaults_enabled, true | |
end | |
def defaults | |
@defaults ||= YAML.load(Rails.root.join('config', 'defaults', self.table_name + '.yml').read) | |
end | |
def without_defaults(&block) | |
defaults_was_enabled = defaults_enabled | |
disable_defaults | |
returning(block.call) { enable_defaults if defaults_was_enabled } | |
end | |
def disable_defaults | |
write_inheritable_attribute :defaults_enabled, false | |
end | |
def enable_defaults | |
write_inheritable_attribute :defaults_enabled, true | |
end | |
end | |
end | |
ActiveRecord::Base.extend(GoodDefaults::ClassMethods) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment