Module: Neuron::View
- Defined in:
- lib/neuron/view.rb
Class Method Summary collapse
Instance Method Summary collapse
- #block_modifiers(block, *modifiers) ⇒ Object
-
#body_attributes ⇒ Object
Make body’s modifiers, based on controller_name and action_name.
- #canonical_link(resource = nil, options = {}) ⇒ Object
-
#canonical_path(resource, options = {}) ⇒ Object
Build canonical path for given resource.
-
#canonical_url(resource, options = {}) ⇒ Object
Build canonical url for given resource.
- #controller_i18n_scope ⇒ Object
- #date(date, options = {}) ⇒ Object
- #html_attributes ⇒ Object
- #human(klass, attribute = nil) ⇒ Object
- #time(time, options = {}) ⇒ Object
- #view_name ⇒ Object
Class Method Details
.setup! ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/neuron/view.rb', line 5 def self.setup! if defined?(::ActionView) ActionView::Base.send(:include, Neuron::View) ActionView::Base.send(:include, Neuron::Navigation::View) ActionView::Base.send(:include, Neuron::Resources::View) if defined?(::InheritedResources) ActionView::Base.send(:include, Neuron::Authorization::View) if defined?(::CanCan) ActionView::Base.send(:include, Neuron::ShowFor::Helper) if defined?(::ShowFor) end end |
Instance Method Details
#block_modifiers(block, *modifiers) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/neuron/view.rb', line 15 def block_modifiers(block, *modifiers) klasses = [block] if ( = modifiers.).any? .each do |modifier, needed| klasses << "#{block}_#{modifier}" if needed end end klasses += modifiers.collect { |modifier| "#{block}_#{modifier}" } {:class => klasses.join(' ')} end |
#body_attributes ⇒ Object
Make body’s modifiers, based on controller_name and action_name
27 28 29 30 31 32 |
# File 'lib/neuron/view.rb', line 27 def body_attributes controller_class = controller_i18n_scope.gsub(/[._]/, '-') block_modifiers("m-#{controller_class}", view_name).tap do |hash| hash[:class] << " m-action_#{view_name}" end end |
#canonical_link(resource = nil, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/neuron/view.rb', line 54 def canonical_link(resource = nil, = {}) href = case resource when Hash url_for(resource) when NilClass request.request_uri.split('?').first else canonical_url(resource, ) end href = "#{request.protocol}#{request.host_with_port}#{href}" unless href =~ /^\w+:\/\// tag(:link, rel: 'canonical', href: href) end |
#canonical_path(resource, options = {}) ⇒ Object
Build canonical path for given resource
50 51 52 |
# File 'lib/neuron/view.rb', line 50 def canonical_path(resource, = {}) canonical_url(resource, .merge(routing_type: :path)) end |
#canonical_url(resource, options = {}) ⇒ Object
Build canonical url for given resource
45 46 47 |
# File 'lib/neuron/view.rb', line 45 def canonical_url(resource, = {}) polymorphic_url(resource, ) end |
#controller_i18n_scope ⇒ Object
71 72 73 |
# File 'lib/neuron/view.rb', line 71 def controller_i18n_scope @controller_i18n_scope ||= controller.controller_path.gsub(%r{/}, '.') end |
#date(date, options = {}) ⇒ Object
84 85 86 |
# File 'lib/neuron/view.rb', line 84 def date(date, = {}) time(date.to_date) end |
#html_attributes ⇒ Object
34 35 36 |
# File 'lib/neuron/view.rb', line 34 def html_attributes {:lang => I18n.locale} end |
#human(klass, attribute = nil) ⇒ Object
38 39 40 41 42 |
# File 'lib/neuron/view.rb', line 38 def human(klass, attribute = nil) @human ||= {} @human[klass] ||= {} @human[klass][attribute] ||= attribute ? klass.human_attribute_name(attribute) : klass.name.human end |
#time(time, options = {}) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/neuron/view.rb', line 75 def time(time, = {}) format = .delete(:format) { :short } title = .delete(:title) do title_format = .delete(:title_format) { :long } l(time, format: title_format) end content_tag(:time, l(time, format: format), .merge(datetime: time.xmlschema, title: title)) end |
#view_name ⇒ Object
67 68 69 |
# File 'lib/neuron/view.rb', line 67 def view_name {create: 'new', update: 'edit'}[action_name] || action_name end |