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



22
23
24
# File 'app/models/routable_record.rb', line 22

def self.controller_action
  @controller_action || default_controller_action
end

.default_controller_actionObject



18
19
20
# File 'app/models/routable_record.rb', line 18

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

.derived_name_fieldObject



34
35
36
# File 'app/models/routable_record.rb', line 34

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

.name_fieldObject



54
55
56
# File 'app/models/routable_record.rb', line 54

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

.route_modelObject



6
7
8
# File 'app/models/routable_record.rb', line 6

def self.route_model
  name.underscore.downcase
end

.set_controller_action(value) ⇒ Object



14
15
16
# File 'app/models/routable_record.rb', line 14

def self.set_controller_action value
  @controller_action = value
end

.set_derived_name_field(value) ⇒ Object



30
31
32
# File 'app/models/routable_record.rb', line 30

def self.set_derived_name_field value
  @derived_name_field = value
end

.set_name_field(value) ⇒ Object



50
51
52
# File 'app/models/routable_record.rb', line 50

def self.set_name_field value
  @name_field = value
end

Instance Method Details

#controller_actionObject



26
27
28
# File 'app/models/routable_record.rb', line 26

def controller_action
  self.class.controller_action
end

#default_url_optionsObject



90
91
92
# File 'app/models/routable_record.rb', line 90

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

#derived_name_fieldObject



38
39
40
# File 'app/models/routable_record.rb', line 38

def derived_name_field
  self.class.derived_name_field
end

#derived_name_field_valueObject



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

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

#name_fieldObject



58
59
60
# File 'app/models/routable_record.rb', line 58

def name_field
  self.class.name_field
end

#name_field_changed?Boolean

Returns:

  • (Boolean)


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

def name_field_changed?
  new_name_value != attributes[name_field]
end

#name_field_valueObject



62
63
64
# File 'app/models/routable_record.rb', line 62

def name_field_value
  attributes[name_field]
end

#new_name_valueObject



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

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



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

def path
  begin
    send("#{route_name}_path")
  rescue NoMethodError
    raise FlowmorRouter::UnroutedRecord.new("#{self.inspect} was not routed.")
  end
end

#populate_nameObject



122
123
124
125
126
# File 'app/models/routable_record.rb', line 122

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

#reload_routesObject



128
129
130
# File 'app/models/routable_record.rb', line 128

def reload_routes
  Rails.application.routes_reloader.reload!
end

#routeObject



86
87
88
# File 'app/models/routable_record.rb', line 86

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

#route_modelObject



10
11
12
# File 'app/models/routable_record.rb', line 10

def route_model
  self.class.route_model
end

#route_nameObject

Raises:

  • (UnroutableRecord)


75
76
77
78
79
80
# File 'app/models/routable_record.rb', line 75

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

#route_name_prefixObject



71
72
73
# File 'app/models/routable_record.rb', line 71

def route_name_prefix
  "#{route_model.pluralize}"
end

#route_prefixObject



82
83
84
# File 'app/models/routable_record.rb', line 82

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

#urlObject



94
95
96
97
98
99
100
# File 'app/models/routable_record.rb', line 94

def url
  begin
    send("#{route_name}_url", default_url_options)
  rescue NoMethodError
    raise FlowmorRouter::UnroutedRecord.new("#{self.inspect} was not routed.")
  end
end