Module: Neuron::Resources::View
- Defined in:
- lib/neuron/resources.rb
Instance Method Summary collapse
- #collection_block(collection = nil, tag = :h1, &block) ⇒ Object
- #collection_list(collection = nil, collection_name = nil) ⇒ Object
- #collection_title(collection = nil, options = {}) ⇒ Object
-
#order(options = {}, html_options = {}) ⇒ Object
Creates a link that alternates between acending and descending.
- #resource_title(resource = nil) ⇒ Object
Instance Method Details
#collection_block(collection = nil, tag = :h1, &block) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/neuron/resources.rb', line 162 def collection_block(collection = nil, tag = :h1, &block) collection ||= self.collection content_tag(:article, class: 'b-collection') do ''.html_safe.tap do |result| result << content_tag(: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 content_tag(: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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/neuron/resources.rb', line 146 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.limit_value pagination = paginate(collection) else start = 1 pagination = '' end content_tag(:ol, render(collection), class: "b-list b-list_#{collection_name.to_s.gsub(/_/, '-')}", start: start) << pagination end |
#collection_title(collection = nil, options = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/neuron/resources.rb', line 131 def collection_title(collection = nil, = {}) collection ||= self.collection tag = .delete(:tag) { :h1 } new_link = .delete(:new_link) { link_to(t("#{controller_i18n_scope}.new", scope: :actions, default: [:new, 'New']), new_resource_path) } = .delete(:i18n) { {count: collection.count} } ''.html_safe.tap do |result| result << title(t("#{controller_i18n_scope}.#{view_name}.title", .merge(default: t("navigation.#{controller_i18n_scope}.#{action_name}", ))), 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
100 101 102 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 |
# File 'lib/neuron/resources.rb', line 100 def order( = {}, = {}) [:class] ||= resource_class [:by] = [:by].to_sym [:as] ||= human([:class], [:by]) asc_orders = Array(params[:ascending]).map(&:to_sym) desc_orders = Array(params[:descending]).map(&:to_sym) ascending = asc_orders.include?([:by]) selected = ascending || desc_orders.include?([:by]) new_scope = ascending ? :descending : :ascending [:title] ||= human([:class], [:by]) ['data-order-by'] = [:by] ['data-order-direction'] = new_scope = {page: params[:page]} [new_scope] = [[:by]] if selected css_classes = [:class] ? [:class].split(/\s+/) : [] if ascending # selected [:as] = "▲ #{[:as]}" css_classes << 'ascending' else # descending selected [:as] = "▼ #{[:as]}" css_classes << 'descending' end [:class] = css_classes.join(' ') end url = [:url] ? url_for([:url].merge()) : collection_path() link_to([:as].html_safe, url, ) end |
#resource_title(resource = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/neuron/resources.rb', line 182 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 << content_tag(: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 |