From f2d8ca033825cc6a651b5d0ecc0eb7b96e7a438d Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Fri, 20 Jan 2017 01:03:06 -0500 Subject: [PATCH] Companies Views and Controllers somewhat implemented - Company index route & new route implemented with views - Company create route has issues - Company route only accessible to admins --- .../app/assets/javascripts/companies.coffee | 3 + .../app/assets/stylesheets/companies.scss | 3 + .../app/controllers/companies_controller.rb | 32 +++++++ Pearlception/app/helpers/companies_helper.rb | 2 + Pearlception/app/models/company.rb | 1 + .../app/views/companies/index.html.erb | 91 +++++++++++++++++++ Pearlception/app/views/companies/new.html.erb | 83 +++++++++++++++++ Pearlception/config/routes.rb | 1 + .../controllers/companies_controller_test.rb | 7 ++ 9 files changed, 223 insertions(+) create mode 100644 Pearlception/app/assets/javascripts/companies.coffee create mode 100644 Pearlception/app/assets/stylesheets/companies.scss create mode 100644 Pearlception/app/controllers/companies_controller.rb create mode 100644 Pearlception/app/helpers/companies_helper.rb create mode 100644 Pearlception/app/views/companies/index.html.erb create mode 100644 Pearlception/app/views/companies/new.html.erb create mode 100644 Pearlception/test/controllers/companies_controller_test.rb diff --git a/Pearlception/app/assets/javascripts/companies.coffee b/Pearlception/app/assets/javascripts/companies.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/Pearlception/app/assets/javascripts/companies.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/Pearlception/app/assets/stylesheets/companies.scss b/Pearlception/app/assets/stylesheets/companies.scss new file mode 100644 index 0000000..412c0f5 --- /dev/null +++ b/Pearlception/app/assets/stylesheets/companies.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Companies controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/Pearlception/app/controllers/companies_controller.rb b/Pearlception/app/controllers/companies_controller.rb new file mode 100644 index 0000000..f50f318 --- /dev/null +++ b/Pearlception/app/controllers/companies_controller.rb @@ -0,0 +1,32 @@ +class CompaniesController < ApplicationController + require 'securerandom' + before_filter :deny_to_visitors + + def new + @company = Company.new + end + + def create + @company = Company.new(company_params) + @company.company_token = SecureRandom.uuid + + if @company.save + redirect_to :action => 'index' + else + flash[:alert] = @company.errors.full_messages.to_sentence + redirect_to :action => 'new' + end + end + + def index + @companies = Company.all + end + + def deny_to_visitors + redirect_to "/signin" unless user_signed_in? && current_user.admin? + end + + def company_params + params.require(:company).permit(:company_name, :company_token) + end +end diff --git a/Pearlception/app/helpers/companies_helper.rb b/Pearlception/app/helpers/companies_helper.rb new file mode 100644 index 0000000..099b151 --- /dev/null +++ b/Pearlception/app/helpers/companies_helper.rb @@ -0,0 +1,2 @@ +module CompaniesHelper +end diff --git a/Pearlception/app/models/company.rb b/Pearlception/app/models/company.rb index c2737b5..3a4fb41 100644 --- a/Pearlception/app/models/company.rb +++ b/Pearlception/app/models/company.rb @@ -1,3 +1,4 @@ class Company < ApplicationRecord belongs_to :user + has_many :runs end diff --git a/Pearlception/app/views/companies/index.html.erb b/Pearlception/app/views/companies/index.html.erb new file mode 100644 index 0000000..8a195dd --- /dev/null +++ b/Pearlception/app/views/companies/index.html.erb @@ -0,0 +1,91 @@ +<%= stylesheet_link_tag "dashboard" %>_ + + + + +
+
+ +
+

Companies

+

Registered Companies

+
+ <%= link_to(new_company_path, :method => :get) do %> + Register a Company + <% end %> + + + + + + + + + + <% if @companies.empty? %> +
#Company IDCompany NameRegistration Serial Key
+
+

No Companies Registered

+
+ <% else %> + + + + <% end %> +
+
+
+
+ <%= javascript_include_tag "bootstrap.min" %>_ + diff --git a/Pearlception/app/views/companies/new.html.erb b/Pearlception/app/views/companies/new.html.erb new file mode 100644 index 0000000..e68b7d4 --- /dev/null +++ b/Pearlception/app/views/companies/new.html.erb @@ -0,0 +1,83 @@ +<%= stylesheet_link_tag "dashboard" %>_ + + + + +
+
+ +
+

Companies

+

Register a New Company

+
+ <%= link_to(companies_path, :method => :get) do %> + Back + <% end %> + <%= form_for @company, url: {action: "create"}, html: {class: "company_form"} do |f| %> +
+ <%= f.text_field :company_name, class: 'form-control', placeholder: 'Company Name' %> +
+
+ <%= f.submit "Create" %> +
+
+ + <% end %> + +
+
+
+ + <%= javascript_include_tag "bootstrap.min" %>_ + diff --git a/Pearlception/config/routes.rb b/Pearlception/config/routes.rb index 625aa85..1a1ae89 100644 --- a/Pearlception/config/routes.rb +++ b/Pearlception/config/routes.rb @@ -2,6 +2,7 @@ Rails.application.routes.draw do root "dashboard#index" resources :runs + resources :companies devise_for :users, :controllers => {:registrations => 'registrations'} devise_scope :users do diff --git a/Pearlception/test/controllers/companies_controller_test.rb b/Pearlception/test/controllers/companies_controller_test.rb new file mode 100644 index 0000000..c3c7d1a --- /dev/null +++ b/Pearlception/test/controllers/companies_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CompaniesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end