mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
Oneboxes should use a sorted array for ordering, not a hash.
This commit is contained in:
parent
e3d30f1366
commit
9d4ecd7ef8
2 changed files with 16 additions and 12 deletions
|
@ -9,7 +9,7 @@ Dir["#{Rails.root}/lib/oneboxer/*_onebox.rb"].each {|f|
|
|||
module Oneboxer
|
||||
extend Oneboxer::Base
|
||||
|
||||
Dir["#{Rails.root}/lib/oneboxer/*_onebox.rb"].each do |f|
|
||||
Dir["#{Rails.root}/lib/oneboxer/*_onebox.rb"].sort.each do |f|
|
||||
add_onebox "Oneboxer::#{Pathname.new(f).basename.to_s.gsub(/\.rb$/, '').classify}".constantize
|
||||
end
|
||||
|
||||
|
@ -19,9 +19,12 @@ module Oneboxer
|
|||
|
||||
# Return a oneboxer for a given URL
|
||||
def self.onebox_for_url(url)
|
||||
matchers.each do |regexp, oneboxer|
|
||||
matchers.each do |matcher|
|
||||
regexp = matcher.regexp
|
||||
klass = matcher.klass
|
||||
|
||||
regexp = regexp.call if regexp.class == Proc
|
||||
return oneboxer.new(url) if url =~ regexp
|
||||
return klass.new(url) if url =~ regexp
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
|
|
@ -28,22 +28,23 @@ module Oneboxer
|
|||
end
|
||||
end
|
||||
|
||||
module Base
|
||||
class Matcher
|
||||
attr_reader :regexp, :klass
|
||||
|
||||
def matchers
|
||||
@matchers ||= {}
|
||||
@matchers
|
||||
def initialize(klass)
|
||||
@klass = klass
|
||||
@regexp = klass.regexp
|
||||
end
|
||||
end
|
||||
|
||||
# Add a matcher
|
||||
def add_matcher(regexp, klass)
|
||||
matchers[regexp] = klass
|
||||
module Base
|
||||
def matchers
|
||||
@matchers ||= []
|
||||
end
|
||||
|
||||
def add_onebox(klass)
|
||||
matchers[klass.regexp] = klass
|
||||
matchers << Matcher.new(klass)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue