mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-18 13:38:24 -04:00
builds views for containers
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
class ContainersController < ApplicationController
|
||||
before_action :authenticate_member!, except: %i(index show)
|
||||
# before_action :authenticate_member!, except: %i(index show)
|
||||
load_and_authorize_resource
|
||||
|
||||
# GET /containers
|
||||
def index
|
||||
@containers = Container.all
|
||||
@containers = Container.all.paginate(page: params[:page])
|
||||
end
|
||||
|
||||
# GET /containers/1
|
||||
|
||||
@@ -42,6 +42,10 @@ class Ability
|
||||
can :read, AlternateName do |an|
|
||||
an.crop.approved?
|
||||
end
|
||||
|
||||
cannot :create, Container
|
||||
cannot :update, Container
|
||||
cannot :destroy, Container
|
||||
end
|
||||
|
||||
def member_abilities(member) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
||||
|
||||
@@ -2,11 +2,11 @@ class Container < ActiveRecord::Base
|
||||
has_many :plots, dependent: :destroy
|
||||
has_many :gardens, through: :plots
|
||||
|
||||
validates :description,
|
||||
format: {
|
||||
with: /\A\w+[\w ()]+\z/
|
||||
},
|
||||
length: { maximum: 255 },
|
||||
presence: true,
|
||||
uniqueness: true
|
||||
validates :description, presence: true, uniqueness: true
|
||||
|
||||
def subtitler(container)
|
||||
num = container.gardens.uniq.count
|
||||
s = (num > 1 || num == 0) ? "s are" : " is"
|
||||
return "#{num.to_s} garden#{s} using this container"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
- if can?(:create, container) && can?(:edit, container) && can?(:destroy, container)
|
||||
.btn-group.container-actions
|
||||
- if can? :edit, container
|
||||
= render 'shared/buttons/edit', path: edit_container_path(container)
|
||||
- if can? :destroy, container
|
||||
.pull-right= render 'shared/buttons/delete', path: container_path(container)
|
||||
.btn-group.container-actions
|
||||
= render 'shared/buttons/edit', path: edit_container_path(container)
|
||||
.pull-right
|
||||
= render 'shared/buttons/delete', path: container_path(container)
|
||||
|
||||
- if can? :create, container
|
||||
= link_to new_container_path, class: 'btn btn-default btn-xs' do
|
||||
%span.glyphicon.glyphicon-grain{ title: "Create new container" }
|
||||
Create new container
|
||||
- if can? :create, container
|
||||
= link_to new_container_path, class: 'btn btn-default btn-xs' do
|
||||
%span.glyphicon.glyphicon-grain{ title: "Create new container" }
|
||||
Create new container
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
= form_for @container do |f|
|
||||
= form_for @container, html: { class: 'form-horizontal', role: "form" } do |f|
|
||||
- if @container.errors.any?
|
||||
#error_explanation
|
||||
%h2= "#{pluralize(@container.errors.count, "error")} prohibited this container from being saved:"
|
||||
@@ -6,8 +6,15 @@
|
||||
- @container.errors.full_messages.each do |message|
|
||||
%li= message
|
||||
|
||||
.field
|
||||
= f.label :description
|
||||
= f.text_field :description
|
||||
.actions
|
||||
= f.submit 'Save'
|
||||
%h2 Basic information
|
||||
|
||||
.form-group
|
||||
= f.label :description, class: 'control-label col-md-2'
|
||||
.col-md-8
|
||||
= f.text_field :description, class: 'form-control'
|
||||
%span.help-block
|
||||
The name for the container, i.e. "organic", in English (required).
|
||||
|
||||
.form-group
|
||||
.form-actions.col-md-offset-2.col-md-8
|
||||
= f.submit 'Save', class: 'btn btn-primary'
|
||||
|
||||
7
app/views/containers/_thumbnail.html.haml
Normal file
7
app/views/containers/_thumbnail.html.haml
Normal file
@@ -0,0 +1,7 @@
|
||||
- cache cache_key_for(Container, container.id) do
|
||||
.thumbnail
|
||||
.container-thumbnail
|
||||
- if container
|
||||
.containerinfo
|
||||
.containername
|
||||
= link_to container.description, container
|
||||
@@ -1,7 +1,4 @@
|
||||
%h1 Editing container
|
||||
- content_for :title, "Edit Container"
|
||||
|
||||
= render 'form'
|
||||
|
||||
= link_to 'Show', @container
|
||||
\|
|
||||
= link_to 'Back', containers_path
|
||||
- if can? :update, @container
|
||||
= render 'form'
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
%h1 Listing containers
|
||||
- content_for :title, 'Containers'
|
||||
|
||||
%table
|
||||
%thead
|
||||
%tr
|
||||
%th Description
|
||||
%th
|
||||
%th
|
||||
%th
|
||||
%p
|
||||
#{ENV['GROWSTUFF_SITE_NAME']} tracks who's growing what, where.
|
||||
View any container page to see which of our members have used it.
|
||||
|
||||
%tbody
|
||||
- @containers.each do |container|
|
||||
%tr
|
||||
%td= container.description
|
||||
%td= link_to 'Show', container
|
||||
%td= link_to 'Edit', edit_container_path(container)
|
||||
%td= link_to 'Destroy', container, method: :delete, data: { confirm: 'Are you sure?' }
|
||||
.row
|
||||
- @containers.each do |container|
|
||||
.col-md-2.six-across
|
||||
= render partial: "thumbnail", locals: { container: container }
|
||||
|
||||
%br
|
||||
- if can? :create, Container
|
||||
%div
|
||||
= link_to 'New Container', new_container_path, class: 'btn btn-primary'
|
||||
|
||||
= link_to 'New Container', new_container_path
|
||||
.pagination
|
||||
= will_paginate @containers
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
%h1 New container
|
||||
- content_for :title, "New Container"
|
||||
|
||||
= render 'form'
|
||||
|
||||
= link_to 'Back', containers_path
|
||||
- if can? :create, @container
|
||||
= render 'form'
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
%p#notice= notice
|
||||
- content_for :title, @container.description.capitalize
|
||||
- content_for :subtitle, @container.subtitler(@container)
|
||||
|
||||
%p
|
||||
%b Description:
|
||||
= @container.description
|
||||
- if can?(:create, @container) && can?(:edit, @container) && can?(:destroy, @container)
|
||||
- content_for :buttonbar do
|
||||
= render 'containers/actions', container: @container
|
||||
|
||||
= link_to 'Edit', edit_container_path(@container)
|
||||
\|
|
||||
= link_to 'Back', containers_path
|
||||
- if @container.gardens.uniq.empty?
|
||||
%p There are no gardens to display.
|
||||
- else
|
||||
- @container.gardens.uniq.each do |garden|
|
||||
= render 'gardens/overview', garden: garden
|
||||
|
||||
11
spec/controllers/containers_controller_spec.rb
Normal file
11
spec/controllers/containers_controller_spec.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ContainersController, type: :controller do
|
||||
include Devise::Test::ControllerHelpers
|
||||
let(:valid_params) { { description: 'My second Container' } }
|
||||
|
||||
let(:container) { FactoryBot.create :container }
|
||||
|
||||
let(:member) { FactoryBot.create(:member) }
|
||||
let(:admin_member) { FactoryBot.create(:admin) }
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
FactoryBot.define do
|
||||
factory :container do
|
||||
description { "MyString" }
|
||||
description { "homemade swamp" }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
FactoryBot.define do
|
||||
factory :plot do
|
||||
garden { nil }
|
||||
container { nil }
|
||||
garden { garden }
|
||||
container { container }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Plot, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
describe Plot do
|
||||
let(:owner) { FactoryBot.create(:member) }
|
||||
let(:garden) { FactoryBot.create(:garden, owner: owner, name: "Magic Beanstalk") }
|
||||
let(:container) { FactoryBot.create(:container, description: "vertical") }
|
||||
let(:plot) { FactoryBot.create(:plot, garden: garden, container: container) }
|
||||
|
||||
context "has valid attributes" do
|
||||
it "should have a garden and container" do
|
||||
plot.garden.should == garden
|
||||
plot.container.should == container
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user