Class: RoutableRecord
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RoutableRecord
- Defined in:
- app/models/routable_record.rb
Class Method Summary collapse
- .controller_action ⇒ Object
- .default_controller_action ⇒ Object
- .derived_name_field ⇒ Object
- .name_field ⇒ Object
- .route_model ⇒ Object
- .set_controller_action(value) ⇒ Object
- .set_derived_name_field(value) ⇒ Object
- .set_name_field(value) ⇒ Object
Instance Method Summary collapse
- #controller_action ⇒ Object
- #default_url_options ⇒ Object
- #derived_name_field ⇒ Object
- #derived_name_field_value ⇒ Object
- #name_field ⇒ Object
- #name_field_changed? ⇒ Boolean
- #name_field_value ⇒ Object
- #new_name_value ⇒ Object
- #path ⇒ Object
- #populate_name ⇒ Object
- #reload_routes ⇒ Object
- #route ⇒ Object
- #route_model ⇒ Object
- #route_name ⇒ Object
- #route_name_prefix ⇒ Object
- #route_prefix ⇒ Object
- #url ⇒ Object
Class Method Details
.controller_action ⇒ Object
20 21 22 |
# File 'app/models/routable_record.rb', line 20 def self.controller_action @controller_action || default_controller_action end |
.default_controller_action ⇒ Object
16 17 18 |
# File 'app/models/routable_record.rb', line 16 def self.default_controller_action "#{route_model.underscore}#show" end |
.derived_name_field ⇒ Object
32 33 34 |
# File 'app/models/routable_record.rb', line 32 def self.derived_name_field @derived_name_field || 'title' end |
.name_field ⇒ Object
52 53 54 |
# File 'app/models/routable_record.rb', line 52 def self.name_field @name_field || 'name' end |
.route_model ⇒ Object
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_action ⇒ Object
24 25 26 |
# File 'app/models/routable_record.rb', line 24 def controller_action self.class.controller_action end |
#default_url_options ⇒ Object
88 89 90 |
# File 'app/models/routable_record.rb', line 88 def { :host => Thread.current[:host], :port => Thread.current[:port] } end |
#derived_name_field ⇒ Object
36 37 38 |
# File 'app/models/routable_record.rb', line 36 def derived_name_field self.class.derived_name_field end |
#derived_name_field_value ⇒ Object
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_field ⇒ Object
56 57 58 |
# File 'app/models/routable_record.rb', line 56 def name_field self.class.name_field end |
#name_field_changed? ⇒ 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_value ⇒ Object
60 61 62 |
# File 'app/models/routable_record.rb', line 60 def name_field_value attributes[name_field] end |
#new_name_value ⇒ Object
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 |
#path ⇒ Object
96 97 98 |
# File 'app/models/routable_record.rb', line 96 def path send("#{route_name}_path") end |
#populate_name ⇒ Object
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_routes ⇒ Object
118 119 120 |
# File 'app/models/routable_record.rb', line 118 def reload_routes Rails.application.routes_reloader.reload! end |
#route ⇒ Object
84 85 86 |
# File 'app/models/routable_record.rb', line 84 def route "#{route_prefix}/#{new_name_value}" end |
#route_model ⇒ Object
8 9 10 |
# File 'app/models/routable_record.rb', line 8 def route_model self.class.route_model end |
#route_name ⇒ Object
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_prefix ⇒ Object
69 70 71 |
# File 'app/models/routable_record.rb', line 69 def route_name_prefix "#{route_model.pluralize}" end |
#route_prefix ⇒ Object
80 81 82 |
# File 'app/models/routable_record.rb', line 80 def route_prefix "/#{route_model.pluralize.gsub('_', '-')}" end |
#url ⇒ Object
92 93 94 |
# File 'app/models/routable_record.rb', line 92 def url send("#{route_name}_url", ) end |