mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
Oneboxes should use a sorted array for ordering, not a hash.
This commit is contained in:
parent
e3d30f1366
commit
9d4ecd7ef8
|
@ -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 New Issue
Block a user