Module: Neuron::View

Defined in:
lib/neuron/view.rb

Class Method Summary collapse

Instance Method Summary collapse

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 (options = modifiers.extract_options!).any?
    options.each do |modifier, needed|
      klasses << "#{block}_#{modifier}" if needed
    end
  end
  klasses += modifiers.collect { |modifier| "#{block}_#{modifier}" }
  {:class => klasses.join(' ')}
end

#body_attributesObject

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


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/neuron/view.rb', line 54

def canonical_link(resource = nil, options = {})
  href = case resource
         when Hash
           url_for(resource)
         when NilClass
           request.request_uri.split('?').first
         else
           canonical_url(resource, options)
         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, options = {})
  canonical_url(resource, options.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, options = {})
  polymorphic_url(resource, options)
end

#controller_i18n_scopeObject



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, options = {})
  time(date.to_date)
end

#html_attributesObject



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, options = {})
  format        = options.delete(:format) { :short }
  title  = options.delete(:title) do
    title_format  = options.delete(:title_format) { :long }
    l(time, format: title_format)
  end
  (:time, l(time, format: format), options.merge(datetime: time.xmlschema, title: title))
end

#view_nameObject



67
68
69
# File 'lib/neuron/view.rb', line 67

def view_name
  {create: 'new', update: 'edit'}[action_name] || action_name
end