Class: RoutableRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/routable_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.controller_actionObject



20
21
22
# File 'app/models/routable_record.rb', line 20

def self.controller_action
  @controller_action || default_controller_action
end

.default_controller_actionObject



16
17
18
# File 'app/models/routable_record.rb', line 16

def self.default_controller_action
  "#{route_model.underscore}#show"
end

.derived_name_fieldObject



32
33
34
# File 'app/models/routable_record.rb', line 32

def self.derived_name_field
  @derived_name_field || 'title'
end

.name_fieldObject



52
53
54
# File 'app/models/routable_record.rb', line 52

def self.name_field
  @name_field || 'name'
end

.route_modelObject



4
5
6
# File 'app/models/routable_record.rb', line 4

def self.route_model
  name.underscore.downcase
end

.set_controller_action(value) ⇒ Object



12
13
14
# File 'app/models/routable_record.rb', line 12

def self.set_controller_action value
  @controller_action = value
end

.set_derived_name_field(value) ⇒ Object



28
29
30
# File 'app/models/routable_record.rb', line 28

def self.set_derived_name_field value
  @derived_name_field = value
end

.set_name_field(value) ⇒ Object



48
49
50
# File 'app/models/routable_record.rb', line 48

def self.set_name_field value
  @name_field = value
end

Instance Method Details

#controller_actionObject



24
25
26
# File 'app/models/routable_record.rb', line 24

def controller_action
  self.class.controller_action
end

#default_url_optionsObject



88
89
90
# File 'app/models/routable_record.rb', line 88

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

#derived_name_fieldObject



36
37
38
# File 'app/models/routable_record.rb', line 36

def derived_name_field
  self.class.derived_name_field
end

#derived_name_field_valueObject



40
41
42
43
44
45
46
# File 'app/models/routable_record.rb', line 40

def derived_name_field_value
  if respond_to? derived_name_field
    send(derived_name_field)
  else
    raise RuntimeError
  end
end

#name_fieldObject



56
57
58
# File 'app/models/routable_record.rb', line 56

def name_field
  self.class.name_field
end

#name_field_changed?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/models/routable_record.rb', line 108

def name_field_changed?
  new_name_value != attributes[name_field]
end

#name_field_valueObject



60
61
62
# File 'app/models/routable_record.rb', line 60

def name_field_value
  attributes[name_field]
end

#new_name_valueObject



100
101
102
103
104
105
106
# File 'app/models/routable_record.rb', line 100

def new_name_value
  if value = derived_name_field_value and !value.blank?
    value.downcase.gsub(/[^\w\s\d\_\-]/,'').gsub(/\s\s+/,' ').gsub(/[^\w\d]/, '-')
  else
    raise FlowmorRouter::UnroutableRecord if value.blank?
  end
end

#pathObject



96
97
98
# File 'app/models/routable_record.rb', line 96

def path
  send("#{route_name}_path")
end

#populate_nameObject



112
113
114
115
116
# File 'app/models/routable_record.rb', line 112

def populate_name
  if attributes[name_field].blank? || name_field_changed?
    send("#{name_field}=", new_name_value)
  end
end

#reload_routesObject



118
119
120
# File 'app/models/routable_record.rb', line 118

def reload_routes
  Rails.application.routes_reloader.reload!
end

#routeObject



84
85
86
# File 'app/models/routable_record.rb', line 84

def route
  "#{route_prefix}/#{new_name_value}"
end

#route_modelObject



8
9
10
# File 'app/models/routable_record.rb', line 8

def route_model
  self.class.route_model
end

#route_nameObject

Raises:

  • (UnroutableRecord)


73
74
75
76
77
78
# File 'app/models/routable_record.rb', line 73

def route_name
  name_suffix = new_name_value
  raise UnroutableRecord if name_suffix.blank?
  
  "#{route_name_prefix}_#{name_suffix}".underscore
end

#route_name_prefixObject



69
70
71
# File 'app/models/routable_record.rb', line 69

def route_name_prefix
  "#{route_model.pluralize}"
end

#route_prefixObject



80
81
82
# File 'app/models/routable_record.rb', line 80

def route_prefix
  "/#{route_model.pluralize.gsub('_', '-')}"
end

#urlObject



92
93
94
# File 'app/models/routable_record.rb', line 92

def url
  send("#{route_name}_url", default_url_options)
end