Created
January 16, 2009 01:54
-
-
Save julik/47759 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
# PostalTimecode is a Timecode extension which supports soft_parse in new() | |
class PostalTimecode < Timecode | |
FH = ActionView::Helpers | |
TC_LENGTH = Timecode.new(0).to_s.length # by example | |
# Guarantee that PostalTimecode.new happens via soft_parse | |
def self.new(from, fps = Timecode::DEFAULT_FPS) | |
from.is_a?(String) ? soft_parse(from, fps) : super(from, fps) | |
end | |
module Helper | |
# Create an input field for timecode entry. Will also carry over the timecode FPS and include a flag | |
# for timecode recognition by the controller | |
def timecode_tag(object_name, method, options = {}) | |
# object_name, method.to_s, self, options.delete(:object) | |
TCInstanceTag.new(object_name, method, self, options.delete(:object)).to_timecode_tag(options) | |
end | |
end | |
# Mixes timecode_field into the Rails FormBuilder | |
module BuilderMethods | |
def timecode_field(method, options = {}) | |
@template.timecode_tag(@object_name, method, objectify_options(options)) | |
end | |
end | |
FH::FormBuilder.send :include, BuilderMethods | |
MP = "prfx" | |
TC_CLASS_NAME = 'tcInput' | |
# Define an InstanceTag that does what we need | |
class TCInstanceTag < FH::InstanceTag | |
# output two inputs for timecode that assemble cleanly into Timecode.new(some_string, fps) | |
def to_timecode_tag(options = {}) | |
timecode = value(object) || Timecode.new(0) | |
options_for_tc = options.merge( | |
:size => TC_LENGTH, :value => timecode.to_s, MP => '(1s)', :class => TC_CLASS_NAME | |
) | |
options_for_fps = { | |
:value => timecode.fps, MP => '(2f)' | |
} | |
to_input_field_tag("text", options_for_tc) + to_input_field_tag("hidden", options_for_fps) | |
end | |
# Redefine add_default_name_and_id to call upon tag_name | |
def add_default_name_and_id(options = nil) | |
mp_suffix = options.delete(MP) | |
if options.has_key?("index") | |
options["name"] ||= tag_name_with_index(options["index"]) | |
options["id"] ||= tag_id_with_index(options["index"]) | |
options.delete("index") | |
elsif defined?(@auto_index) | |
options["name"] ||= tag_name_with_index(@auto_index) | |
options["id"] ||= tag_id_with_index(@auto_index) | |
else | |
options["name"] ||= tag_name(mp_suffix) + (options.has_key?('multiple') ? '[]' : '') | |
options["id"] ||= tag_id(mp_suffix) | |
end | |
end | |
# Dupe sanitized_method_name because otherwise it memoizes and appends the multiparam suffixes | |
def sanitized_method_name | |
super.dup | |
end | |
# Override tag_name to pass the multiparameter suffixes | |
def tag_name(multiparameter_suffix = nil) | |
tag_name = sanitized_method_name | |
tag_name << multiparameter_suffix if multiparameter_suffix | |
"#{@object_name}[#{tag_name}]" | |
end | |
# Override tag_id to provied the handle to multiparameter parts | |
def tag_id(multiparameter_suffix = nil) | |
[super(), multiparameter_suffix].compact.join('_') | |
end | |
end | |
end | |
class String | |
def to_tc(at_fps = Timecode::DEFAULT_FPS) | |
Timecode.parse(self, at_fps) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment