Module: Neuron::Resources::View

Defined in:
lib/neuron/resources.rb

Instance Method Summary collapse

Instance Method Details

#collection_block(collection = nil, tag = :h1, &block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/neuron/resources.rb', line 163

def collection_block(collection = nil, tag = :h1, &block)
  collection ||= self.collection
  (:article, class: 'b-collection') do
    ''.html_safe.tap do |result|
      result << (:header, collection_title(collection, tag: tag), class: 'b-collection__header')
      if block_given?
        result << capture(&block)
      else
        result << if collection.any?
                    collection_list(collection)
                  else
                    (:p, t(:no_entries,
                                      scope: [:resources, :collection, :no_entries],
                                      default: [controller_i18n_scope.to_sym, :all]))
                  end
      end
    end
  end
end

#collection_list(collection = nil, collection_name = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/neuron/resources.rb', line 147

def collection_list(collection = nil, collection_name = nil)
  collection      ||= self.collection
  collection_name ||= self.resource_collection_name
  if collection.respond_to?(:total_pages)
    start = 1 + (collection.current_page - 1) * collection.per_page
    pagination = will_paginate(collection)
  else
    start = 1
    pagination = ''
  end
  (:ol,
              render(collection),
              class: "b-list b-list_#{collection_name.to_s.gsub(/_/, '-')}",
              start: start) << pagination
end

#collection_title(collection = nil, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/neuron/resources.rb', line 132

def collection_title(collection = nil, options = {})
  collection ||= self.collection
  tag           = options.delete(:tag)      { :h1 }
  new_link      = options.delete(:new_link) { link_to(t("#{controller_i18n_scope}.new", scope: :actions, default: [:new, 'New']), new_resource_path) }
  i18n_options  = options.delete(:i18n)     { {count: collection.count} }
  ''.html_safe.tap do |result|
    result << title(t("#{controller_i18n_scope}.#{view_name}.title",
                      options.merge(default: t("navigation.#{controller_i18n_scope}.#{action_name}", i18n_options))),
                    tag: tag)
    if new_link && can?(:create, resource_class) && controller.respond_to?(:create)
      result << new_link
    end
  end
end

#order(options = {}, html_options = {}) ⇒ Object

Creates a link that alternates between acending and descending

Parameters:

  • options (Hash) (defaults to: {})

    options hash

  • html_options (Hash) (defaults to: {})

    html options hash

Options Hash (options):

  • :by (Symbol, String)

    the name of the columnt to sort by

  • :as (String)

    the text used in link, defaults to human_method_name of :by

  • :url (String)

    url options when order link does not correspond to current collection_path



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/neuron/resources.rb', line 103

def order(options = {}, html_options = {})
  options[:class] ||= resource_class
  options[:by] = options[:by].to_sym
  options[:as] ||= human(options[:class], options[:by])
  html_options[:title] ||= human(options[:class], options[:by])
  asc_orders  = Array(params[:ascending]).map(&:to_sym)
  desc_orders = Array(params[:descending]).map(&:to_sym)
  ascending = asc_orders.include?(options[:by])
  selected  = ascending || desc_orders.include?(options[:by])
  new_scope = ascending ? :descending : :ascending
  url_options = {page: params[:page]}
  url_options[new_scope] = [options[:by]]

  if selected
    css_classes = html_options[:class] ? html_options[:class].split(/\s+/) : []
    if ascending # selected
      options[:as] = "&#9650;&nbsp;#{options[:as]}"
      css_classes << 'ascending'
    else # descending selected
      options[:as] = "&#9660;&nbsp;#{options[:as]}"
      css_classes << 'descending'
    end
    html_options[:class] = css_classes.join(' ')
  end
  url = options[:url] ? url_for(options[:url].merge(url_options)) : collection_path(url_options)

  link_to(options[:as].html_safe, url, html_options)
end

#resource_title(resource = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/neuron/resources.rb', line 183

def resource_title(resource = nil)
  resource ||= self.resource
  action = action_name.to_sym
  ''.html_safe.tap do |result|
    result << title(nil,
                    resource: link_to(resource, canonical_path(resource)),
                    default:  resource.to_s)
    if (action == :show) && can?(:update, resource) && controller.respond_to?(:edit)
      result << (:sup,
        link_to(t(:edit,
                  object: resource,
                  scope: :actions,
                  default: [:"#{controller_i18n_scope}.edit", :edit, 'Edit']), edit_resource_path))
    end
    #if (action == :edit) && can?(:update, resource)
      #result << content_tag(:sup, link_to(t(:edit, :scope => "actions.#{controller_i18n_scope}"), edit_resource_path))
    #end
  end
end