From f2d8ca033825cc6a651b5d0ecc0eb7b96e7a438d Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Fri, 20 Jan 2017 01:03:06 -0500 Subject: [PATCH 1/3] 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 From ace1290a76920916d01eb08776380d1e42c3e6d9 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Fri, 20 Jan 2017 02:16:45 -0500 Subject: [PATCH 2/3] Changed Index title of dashboard for admins --- .../app/views/dashboard/index.html.erb | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/Pearlception/app/views/dashboard/index.html.erb b/Pearlception/app/views/dashboard/index.html.erb index fdaf05d..6d231d2 100644 --- a/Pearlception/app/views/dashboard/index.html.erb +++ b/Pearlception/app/views/dashboard/index.html.erb @@ -84,11 +84,18 @@ --> -

Recent Results

+ <% if current_user.admin? %> +

Recent Results From All Companies

+ <% else %> +

Recent Results

+ <% end %>
+ <% if current_user.admin? %> + + <% end %> @@ -99,6 +106,59 @@ + <% if current_user.admin? %> + <% @results.each_slice(1) do |row| %> + + <% row.each do |result| + run = Run.find(result.id) + #company = Company.find(run.company_id) + %> + + + + + + + + + + + + + + + + + + + + + + + + + <% end %> + + <% end %> + <% else %> <% @results.each_slice(1) do |row| %> <% row.each do |result| @@ -141,6 +201,7 @@ <% end %> <% end %> + <% end %>
CompanyRun Date Location Harvest Time
+ <%= #company.company_name + "Fake Industries" %> + + <%= run.runDate %> + + <%= run.location ? run.location : "" %> + + <%= "" %> + + + <%= run.supplier ? run.supplier : "" %> + + <%= run.distributor ? run.distributor : "" %> + + <%= result.total %> + + <%= run.other ? run.other : "" %> +
From 7c6cf0f38d9fe39dffb3675758523c6e8d4ec0f9 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Fri, 20 Jan 2017 02:40:43 -0500 Subject: [PATCH 3/3] Create implemented Company - Create route and view implemented with minimal info - index implemented and shows number of registered companies --- Pearlception/app/models/company.rb | 2 +- Pearlception/app/models/user.rb | 2 +- .../app/views/companies/index.html.erb | 26 ++++++- .../app/views/dashboard/index.html.erb | 70 +++++++++---------- 4 files changed, 60 insertions(+), 40 deletions(-) diff --git a/Pearlception/app/models/company.rb b/Pearlception/app/models/company.rb index 3a4fb41..fdc526f 100644 --- a/Pearlception/app/models/company.rb +++ b/Pearlception/app/models/company.rb @@ -1,4 +1,4 @@ class Company < ApplicationRecord - belongs_to :user + has_many :users has_many :runs end diff --git a/Pearlception/app/models/user.rb b/Pearlception/app/models/user.rb index 0a39c7f..a44f648 100644 --- a/Pearlception/app/models/user.rb +++ b/Pearlception/app/models/user.rb @@ -1,5 +1,5 @@ class User < ApplicationRecord - has_one :company + belongs_to :company has_many :runs # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable diff --git a/Pearlception/app/views/companies/index.html.erb b/Pearlception/app/views/companies/index.html.erb index 8a195dd..b505cdf 100644 --- a/Pearlception/app/views/companies/index.html.erb +++ b/Pearlception/app/views/companies/index.html.erb @@ -59,15 +59,14 @@

Companies

-

Registered Companies

-
+

Registered Companies: <%= @companies.count %>

+
<%= link_to(new_company_path, :method => :get) do %> Register a Company <% end %> - @@ -80,6 +79,27 @@ <% else %> + <% @companies.each_slice(1) do |row| %> + + <% row.each do |company|%> + + + + + + + + + + <% end %> + + <% end %>
# Company ID Company Name Registration Serial Key
+ <%= company.id %> + + <%= company.company_name %> + + <%= company.company_token %> +
<% end %> diff --git a/Pearlception/app/views/dashboard/index.html.erb b/Pearlception/app/views/dashboard/index.html.erb index 6d231d2..accc7ef 100644 --- a/Pearlception/app/views/dashboard/index.html.erb +++ b/Pearlception/app/views/dashboard/index.html.erb @@ -159,48 +159,48 @@ <% end %> <% else %> - <% @results.each_slice(1) do |row| %> - - <% row.each do |result| - run = Run.find(result.id) %> - - - <%= run.runDate %> - + <% @results.each_slice(1) do |row| %> + + <% row.each do |result| + run = Run.find(result.id) %> + + + <%= run.runDate %> + - - - <%= run.location ? run.location : "" %> - + + + <%= run.location ? run.location : "" %> + - - - <%= "" %> - + + + <%= "" %> + - - + + - <%= run.supplier ? run.supplier : "" %> - + <%= run.supplier ? run.supplier : "" %> + - - - <%= run.distributor ? run.distributor : "" %> - + + + <%= run.distributor ? run.distributor : "" %> + - - - <%= result.total %> - + + + <%= result.total %> + - - - <%= run.other ? run.other : "" %> - - <% end %> - - <% end %> + + + <%= run.other ? run.other : "" %> + + <% end %> + + <% end %> <% end %>