Class: FlowmorRouter::RouterClasses
- Inherits:
-
Object
- Object
- FlowmorRouter::RouterClasses
- Defined in:
- lib/flowmor_router/router_classes.rb
Overview
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 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 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 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 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 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 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
Constant Summary collapse
- @@router_classes =
[]
Instance Attribute Summary collapse
-
#actor ⇒ Object
readonly
Returns the value of attribute actor.
-
#controller_action ⇒ Object
readonly
Returns the value of attribute controller_action.
-
#custom_name ⇒ Object
readonly
Returns the value of attribute custom_name.
-
#custom_route ⇒ Object
readonly
Returns the value of attribute custom_route.
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name_field_attribute ⇒ Object
readonly
Returns the value of attribute name_field_attribute.
-
#no_conflict ⇒ Object
readonly
Returns the value of attribute no_conflict.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
-
#title_field_attribute ⇒ Object
readonly
Returns the value of attribute title_field_attribute.
Class Method Summary collapse
- .each(&block) ⇒ Object
- .register(actor, model, options) ⇒ Object
- .router_classes ⇒ Object
- .unregister(model) ⇒ Object
Instance Method Summary collapse
- #default_url_options ⇒ Object
-
#initialize(actor, model, options) ⇒ RouterClasses
constructor
A new instance of RouterClasses.
- #name(record) ⇒ Object
- #name_field(record) ⇒ Object
- #named_instance(method = "router_class") ⇒ Object
- #no_conflict_model_name ⇒ Object
- #path(record) ⇒ Object
- #path_method_name ⇒ Object
- #routable ⇒ Object
- #route_base_name ⇒ Object
- #route_base_path ⇒ Object
- #route_name(record, verb = nil) ⇒ Object
- #route_name_method_name ⇒ Object
- #route_name_prefix(record) ⇒ Object
- #route_name_suffix(record) ⇒ Object
- #route_path(record) ⇒ Object
- #route_path_prefix(record) ⇒ Object
- #route_path_suffix(record) ⇒ Object
- #route_prefix(record) ⇒ Object
- #route_suffix(record) ⇒ Object
- #scope_name ⇒ Object
- #title_field(record) ⇒ Object
- #to_param(value) ⇒ Object
- #url(record) ⇒ Object
- #url_method_name ⇒ Object
- #verb_prefix(verb) ⇒ Object
Constructor Details
#initialize(actor, model, options) ⇒ RouterClasses
Returns a new instance of RouterClasses.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/flowmor_router/router_classes.rb', line 121 def initialize actor, model, @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 = [:controller_action] || "#{actor}#show" @name_field_attribute = [:name_field] || :name @title_field_attribute = [:title_field] || :title @custom_route = [:route] @custom_name = [:name] @prefix = [:prefix] if [:prefix] @suffix = [:suffix] if [:suffix] @delimiter = [:delimiter] || "-" end |
Instance Attribute Details
#actor ⇒ Object (readonly)
Returns the value of attribute actor.
116 117 118 |
# File 'lib/flowmor_router/router_classes.rb', line 116 def actor @actor end |
#controller_action ⇒ Object (readonly)
Returns the value of attribute controller_action.
117 118 119 |
# File 'lib/flowmor_router/router_classes.rb', line 117 def controller_action @controller_action end |
#custom_name ⇒ Object (readonly)
Returns the value of attribute custom_name.
119 120 121 |
# File 'lib/flowmor_router/router_classes.rb', line 119 def custom_name @custom_name end |
#custom_route ⇒ Object (readonly)
Returns the value of attribute custom_route.
119 120 121 |
# File 'lib/flowmor_router/router_classes.rb', line 119 def custom_route @custom_route end |
#delimiter ⇒ Object (readonly)
Returns the value of attribute delimiter.
119 120 121 |
# File 'lib/flowmor_router/router_classes.rb', line 119 def delimiter @delimiter end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
116 117 118 |
# File 'lib/flowmor_router/router_classes.rb', line 116 def model @model end |
#name_field_attribute ⇒ Object (readonly)
Returns the value of attribute name_field_attribute.
118 119 120 |
# File 'lib/flowmor_router/router_classes.rb', line 118 def name_field_attribute @name_field_attribute end |
#no_conflict ⇒ Object (readonly)
Returns the value of attribute no_conflict.
117 118 119 |
# File 'lib/flowmor_router/router_classes.rb', line 117 def no_conflict @no_conflict end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
117 118 119 |
# File 'lib/flowmor_router/router_classes.rb', line 117 def prefix @prefix end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
117 118 119 |
# File 'lib/flowmor_router/router_classes.rb', line 117 def suffix @suffix end |
#title_field_attribute ⇒ Object (readonly)
Returns the value of attribute title_field_attribute.
118 119 120 |
# File 'lib/flowmor_router/router_classes.rb', line 118 def title_field_attribute @title_field_attribute end |
Class Method Details
.each(&block) ⇒ Object
112 113 114 |
# File 'lib/flowmor_router/router_classes.rb', line 112 def self.each &block @@router_classes.each{ |rc| yield rc } end |
.register(actor, model, options) ⇒ Object
96 97 98 99 100 |
# File 'lib/flowmor_router/router_classes.rb', line 96 def self.register actor, model, router_class = RouterClasses.new(actor, model, ) @@router_classes << router_class return router_class end |
.router_classes ⇒ Object
108 109 110 |
# File 'lib/flowmor_router/router_classes.rb', line 108 def self.router_classes @@router_classes end |
.unregister(model) ⇒ Object
102 103 104 105 106 |
# 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 |
Instance Method Details
#default_url_options ⇒ Object
142 143 144 |
# File 'lib/flowmor_router/router_classes.rb', line 142 def { :host => Thread.current[:host], :port => Thread.current[:port] } end |
#name(record) ⇒ Object
184 185 186 187 188 189 |
# 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 |
#name_field(record) ⇒ Object
176 177 178 |
# File 'lib/flowmor_router/router_classes.rb', line 176 def name_field record record.send(name_field_attribute) end |
#named_instance(method = "router_class") ⇒ Object
172 173 174 |
# File 'lib/flowmor_router/router_classes.rb', line 172 def named_instance method = "router_class" "flowmor_#{route_base_name}_#{method}" end |
#no_conflict_model_name ⇒ Object
146 147 148 149 |
# 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 |
#path(record) ⇒ Object
250 251 252 253 254 255 256 |
# 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 |
#path_method_name ⇒ Object
246 247 248 |
# File 'lib/flowmor_router/router_classes.rb', line 246 def path_method_name no_conflict ? "#{actor}_path" : "path" end |
#routable ⇒ Object
163 164 165 |
# File 'lib/flowmor_router/router_classes.rb', line 163 def routable @model.send scope_name end |
#route_base_name ⇒ Object
151 152 153 |
# File 'lib/flowmor_router/router_classes.rb', line 151 def route_base_name @route_base_name ||= [no_conflict_model_name, actor].compact.join("_") end |
#route_base_path ⇒ Object
155 156 157 |
# File 'lib/flowmor_router/router_classes.rb', line 155 def route_base_path @route_base_path ||= "/" + actor end |
#route_name(record, verb = nil) ⇒ Object
226 227 228 |
# 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 |
#route_name_method_name ⇒ Object
238 239 240 |
# File 'lib/flowmor_router/router_classes.rb', line 238 def route_name_method_name no_conflict ? "#{route_base_name}_route_name" : "route_name" end |
#route_name_prefix(record) ⇒ Object
211 212 213 214 |
# File 'lib/flowmor_router/router_classes.rb', line 211 def route_name_prefix record return if @prefix.nil? route_prefix(record).join("_") + "_" end |
#route_name_suffix(record) ⇒ Object
196 197 198 199 |
# File 'lib/flowmor_router/router_classes.rb', line 196 def route_name_suffix record return if @suffix.nil? route_suffix(record).join("_") + "_" end |
#route_path(record) ⇒ Object
230 231 232 233 234 235 236 |
# 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 |
#route_path_prefix(record) ⇒ Object
216 217 218 219 |
# File 'lib/flowmor_router/router_classes.rb', line 216 def route_path_prefix record return if @prefix.nil? "/" + route_prefix(record).join("/") end |
#route_path_suffix(record) ⇒ Object
201 202 203 204 |
# File 'lib/flowmor_router/router_classes.rb', line 201 def route_path_suffix record return if @suffix.nil? "/" + route_suffix(record).join("/") end |
#route_prefix(record) ⇒ Object
206 207 208 209 |
# 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 |
#route_suffix(record) ⇒ Object
191 192 193 194 |
# 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 |
#scope_name ⇒ Object
159 160 161 |
# File 'lib/flowmor_router/router_classes.rb', line 159 def scope_name "flowmor_#{route_base_name}_routable" end |
#title_field(record) ⇒ Object
180 181 182 |
# File 'lib/flowmor_router/router_classes.rb', line 180 def title_field record record.send(title_field_attribute) end |
#to_param(value) ⇒ Object
167 168 169 170 |
# 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 |
#url(record) ⇒ Object
258 259 260 261 262 263 264 |
# File 'lib/flowmor_router/router_classes.rb', line 258 def url record begin Rails.application.routes.url_helpers.send("#{route_name(record)}_url", ) rescue NoMethodError raise FlowmorRouter::UnroutedRecord.new("[#{route_name(record)}] #{record.inspect} was not routed.") end end |
#url_method_name ⇒ Object
242 243 244 |
# File 'lib/flowmor_router/router_classes.rb', line 242 def url_method_name no_conflict ? "#{actor}_url" : "url" end |
#verb_prefix(verb) ⇒ Object
221 222 223 224 |
# 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 |