Class: FlowmorRouter::RouterClasses

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, 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

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.



116
117
118
# File 'lib/flowmor_router/router_classes.rb', line 116

def actor
  @actor
end

#controller_actionObject (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_nameObject (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_routeObject (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

#delimiterObject (readonly)

Returns the value of attribute delimiter.



119
120
121
# File 'lib/flowmor_router/router_classes.rb', line 119

def delimiter
  @delimiter
end

#modelObject (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_attributeObject (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_conflictObject (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

#prefixObject (readonly)

Returns the value of attribute prefix.



117
118
119
# File 'lib/flowmor_router/router_classes.rb', line 117

def prefix
  @prefix
end

#suffixObject (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_attributeObject (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, options
  router_class = RouterClasses.new(actor, model, options)
  @@router_classes << router_class
  return router_class
end

.router_classesObject



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_optionsObject



142
143
144
# File 'lib/flowmor_router/router_classes.rb', line 142

def default_url_options
  { :host => Thread.current[:host], :port => Thread.current[:port] }
end

#name(record) ⇒ Object

Raises:



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_nameObject



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_nameObject



246
247
248
# File 'lib/flowmor_router/router_classes.rb', line 246

def path_method_name
  no_conflict ? "#{actor}_path" : "path"
end

#routableObject



163
164
165
# File 'lib/flowmor_router/router_classes.rb', line 163

def routable
  @model.send scope_name
end

#route_base_nameObject



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_pathObject



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_nameObject



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_nameObject



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", default_url_options)
  rescue NoMethodError
    raise FlowmorRouter::UnroutedRecord.new("[#{route_name(record)}] #{record.inspect} was not routed.")
  end
end

#url_method_nameObject



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