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
22 23 24 |
# File 'app/models/routable_record.rb', line 22 def self.controller_action @controller_action || default_controller_action end |
.default_controller_action ⇒ Object
18 19 20 |
# File 'app/models/routable_record.rb', line 18 def self.default_controller_action "#{route_model.underscore}#show" end |
.derived_name_field ⇒ Object
34 35 36 |
# File 'app/models/routable_record.rb', line 34 def self.derived_name_field @derived_name_field || 'title' end |
.name_field ⇒ Object
54 55 56 |
# File 'app/models/routable_record.rb', line 54 def self.name_field @name_field || 'name' end |
.route_model ⇒ Object
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_action ⇒ Object
26 27 28 |
# File 'app/models/routable_record.rb', line 26 def controller_action self.class.controller_action end |
#default_url_options ⇒ Object
90 91 92 |
# File 'app/models/routable_record.rb', line 90 def { :host => Thread.current[:host], :port => Thread.current[:port] } end |
#derived_name_field ⇒ Object
38 39 40 |
# File 'app/models/routable_record.rb', line 38 def derived_name_field self.class.derived_name_field end |
#derived_name_field_value ⇒ Object
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_field ⇒ Object
58 59 60 |
# File 'app/models/routable_record.rb', line 58 def name_field self.class.name_field end |
#name_field_changed? ⇒ 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_value ⇒ Object
62 63 64 |
# File 'app/models/routable_record.rb', line 62 def name_field_value attributes[name_field] end |
#new_name_value ⇒ Object
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 |
#path ⇒ Object
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_name ⇒ Object
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_routes ⇒ Object
128 129 130 |
# File 'app/models/routable_record.rb', line 128 def reload_routes Rails.application.routes_reloader.reload! end |
#route ⇒ Object
86 87 88 |
# File 'app/models/routable_record.rb', line 86 def route "#{route_prefix}/#{new_name_value}" end |
#route_model ⇒ Object
10 11 12 |
# File 'app/models/routable_record.rb', line 10 def route_model self.class.route_model end |
#route_name ⇒ Object
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_prefix ⇒ Object
71 72 73 |
# File 'app/models/routable_record.rb', line 71 def route_name_prefix "#{route_model.pluralize}" end |
#route_prefix ⇒ Object
82 83 84 |
# File 'app/models/routable_record.rb', line 82 def route_prefix "/#{route_model.pluralize.gsub('_', '-')}" end |
#url ⇒ Object
94 95 96 97 98 99 100 |
# File 'app/models/routable_record.rb', line 94 def url begin send("#{route_name}_url", ) rescue NoMethodError raise FlowmorRouter::UnroutedRecord.new("#{self.inspect} was not routed.") end end |