class FlowmorRouter::RouterClasses
class Post < ActiveRecord::Base
acts_as_routable
end
route_prefix
= ” actor = posts route_name
= posts_title_me_silly route_path
= posts_title_me_silly_path route_url = posts_title_me_silly_url @post.path = /posts/title-me-silly @post.url = example.com/posts/title-me-silly
class Post < ActiveRecord::Base
acts_as_routable :ramblings
end
route_prefix
= ” actor = ramblings route_name
= ramblings_title_me_silly route_path
= ramblings_title_me_silly_path route_url = ramblings_title_me_silly_url @post.path = /ramblings/title-me-silly @post.url = example.com/ramblings/title-me-silly
class Post < ActiveRecord::Base
acts_as_routable :ramblings, prefix: :posts
end
route_prefix
= posts actor = ramblings route_name
= posts_ramblings_title_me_silly route_path
= posts_ramblings_title_me_silly_path route_url = posts_ramblings_title_me_silly_url @post.path = /posts/ramblings/title-me-silly @post.url = example.com/posts/ramblings/title-me-silly
class Post < ActiveRecord::Base
acts_as_routable :ramblings, prefix: [:blog, :posts]
end
route_prefix
= blog_posts actor = ramblings route_name
= blog_posts_ramblings_title_me_silly route_path
= blog_posts_ramblings_title_me_silly_path route_url = blog_posts_ramblings_title_me_silly_url @post.path = /blog/posts/ramblings/title-me-silly @post.url = example.com/blog/posts/ramblings/title-me-silly
class Post < ActiveRecord::Base
belongs_to :category acts_as_routable :ramblings, prefix: -> { category.name }
end
route_prefix
= blog_posts actor = ramblings route_name
= silly_category_ramblings_title_me_silly route_path
= silly_category_ramblings_title_me_silly_path route_url = silly_category_ramblings_title_me_silly_url @post.path = /silly-category/posts/ramblings/title-me-silly @post.url = example.com/silly-category/posts/ramblings/title-me-silly
class Post < ActiveRecord::Base
acts_as_routable acts_as_routable :archive, prefix: [:posts]
end
route_prefix
= ” actor = posts route_name
= posts_title_me_silly route_path
= posts_title_me_silly_path route_url = posts_title_me_silly_url @post.path = /posts/title-me-silly @post.url = example.com/posts/title-me-silly
AND
route_prefix
= posts actor = archive route_name
= posts_archive_title_me_silly route_path
= posts_archive_title_me_silly_path route_url = posts_archive_title_me_silly_url @post.posts_archive_path = /posts/archive/title-me-silly @post.posts_archive_url = example.com/posts/archive/title-me-silly
Attributes
Public Class Methods
# File lib/flowmor_router/router_classes.rb, line 112 def self.each &block @@router_classes.each{ |rc| yield rc } end
# File lib/flowmor_router/router_classes.rb, line 121 def initialize actor, model, options @actor = actor.to_s @model = model @name_field_attribute = :name @title_field_attribute = :title previous = @@router_classes.detect{|rc| rc.model == @model} raise DuplicateRouterActors.new("duplicate actors registered!") if previous.try(:route_route) == actor @no_conflict = !!previous @controller_action = options[:controller_action] || "#{actor}#show" @name_field_attribute = options[:name_field] || :name @title_field_attribute = options[:title_field] || :title @custom_route = options[:route] @custom_name = options[:name] @prefix = options[:prefix] if options[:prefix] @suffix = options[:suffix] if options[:suffix] @delimiter = options[:delimiter] || "-" end
# File lib/flowmor_router/router_classes.rb, line 96 def self.register actor, model, options router_class = RouterClasses.new(actor, model, options) @@router_classes << router_class return router_class end
# File lib/flowmor_router/router_classes.rb, line 108 def self.router_classes @@router_classes end
# File lib/flowmor_router/router_classes.rb, line 102 def self.unregister model @@router_classes.select{ |s| s.model == model }.each do |item| @@router_classes.delete(item) end end
Public Instance Methods
# File lib/flowmor_router/router_classes.rb, line 142 def default_url_options { :host => Thread.current[:host], :port => Thread.current[:port] } end
# File lib/flowmor_router/router_classes.rb, line 184 def name record name = record.send(custom_name) if custom_name name ||= name_field(record) || to_param(title_field(record)) raise UnroutableRecord if name.to_s.strip.blank? return name end
# File lib/flowmor_router/router_classes.rb, line 176 def name_field record record.send(name_field_attribute) end
# File lib/flowmor_router/router_classes.rb, line 172 def named_instance method = "router_class" "flowmor_#{route_base_name}_#{method}" end
# File lib/flowmor_router/router_classes.rb, line 146 def no_conflict_model_name return unless no_conflict @model.name.underscore.downcase.split("/").last.pluralize end
# File lib/flowmor_router/router_classes.rb, line 250 def path record begin Rails.application.routes.url_helpers.send("#{route_name(record)}_path") rescue NoMethodError raise FlowmorRouter::UnroutedRecord.new("[#{route_name(record)}] #{record.inspect} was not routed.") end end
# File lib/flowmor_router/router_classes.rb, line 246 def path_method_name no_conflict ? "#{actor}_path" : "path" end
# File lib/flowmor_router/router_classes.rb, line 163 def routable @model.send scope_name end
# File lib/flowmor_router/router_classes.rb, line 151 def route_base_name @route_base_name ||= [no_conflict_model_name, actor].compact.join("_") end
# File lib/flowmor_router/router_classes.rb, line 155 def route_base_path @route_base_path ||= "/" + actor end
# File lib/flowmor_router/router_classes.rb, line 226 def route_name record, verb=nil "#{verb_prefix(verb)}#{route_name_prefix(record)}#{route_base_name}_#{route_name_suffix(record)}#{name(record)}".underscore.parameterize("_") end
# File lib/flowmor_router/router_classes.rb, line 238 def route_name_method_name no_conflict ? "#{route_base_name}_route_name" : "route_name" end
# File lib/flowmor_router/router_classes.rb, line 211 def route_name_prefix record return if @prefix.nil? route_prefix(record).join("_") + "_" end
# File lib/flowmor_router/router_classes.rb, line 196 def route_name_suffix record return if @suffix.nil? route_suffix(record).join("_") + "_" end
# File lib/flowmor_router/router_classes.rb, line 230 def route_path record if custom_route record.send(custom_route) else "#{route_path_prefix(record)}#{route_base_path}#{route_path_suffix(record)}/#{name(record)}" end end
# File lib/flowmor_router/router_classes.rb, line 216 def route_path_prefix record return if @prefix.nil? "/" + route_prefix(record).join("/") end
# File lib/flowmor_router/router_classes.rb, line 201 def route_path_suffix record return if @suffix.nil? "/" + route_suffix(record).join("/") end
# File lib/flowmor_router/router_classes.rb, line 206 def route_prefix record return if @prefix.nil? Array(@prefix.is_a?(Proc) ? record.send(prefix.call) : @prefix) end
# File lib/flowmor_router/router_classes.rb, line 191 def route_suffix record return if @suffix.nil? Array(@suffix.is_a?(Proc) ? record.send(suffix.call) : @suffix) end
# File lib/flowmor_router/router_classes.rb, line 159 def scope_name "flowmor_#{route_base_name}_routable" end
# File lib/flowmor_router/router_classes.rb, line 180 def title_field record record.send(title_field_attribute) end
# File lib/flowmor_router/router_classes.rb, line 167 def to_param value return unless value value.downcase.gsub(/[^\w\s\d\_\-]/,'').gsub(/\s\s+/,' ').gsub(/[^\w\d]/, delimiter) end
# File lib/flowmor_router/router_classes.rb, line 258 def url record begin Rails.application.routes.url_helpers.send("#{route_name(record)}_url", default_url_options) rescue NoMethodError raise FlowmorRouter::UnroutedRecord.new("[#{route_name(record)}] #{record.inspect} was not routed.") end end
# File lib/flowmor_router/router_classes.rb, line 242 def url_method_name no_conflict ? "#{actor}_url" : "url" end
# File lib/flowmor_router/router_classes.rb, line 221 def verb_prefix verb return "#{verb}_" if verb "#{controller_action.keys.first}_" if controller_action.is_a? Hash end