From 55d899cb0a5b048fc7c420c73704983185d4e9dc Mon Sep 17 00:00:00 2001
From: Mateus Rambo Strey <mars11@inf.ufpr.br>
Date: Mon, 9 Nov 2015 11:16:08 -0200
Subject: [PATCH] add institutions to management

---
 .../management/institutions_controller.rb     | 74 +++++++++++++++++++
 app/controllers/management_controller.rb      |  9 ++-
 app/models/role.rb                            | 12 +++
 app/models/user.rb                            |  8 ++
 .../orient_db/institution_repository.rb       |  7 +-
 .../management/institutions/_form.html.erb    | 55 ++++++++++++++
 .../management/institutions/edit.html.erb     |  6 ++
 .../management/institutions/index.html.erb    | 31 ++++++++
 .../institutions/index.json.jbuilder          |  4 +
 .../management/institutions/new.html.erb      |  8 ++
 .../management/institutions/show.html.erb     |  4 +
 .../institutions/show.json.jbuilder           |  1 +
 .../shared/management/_nav_menu.html.erb      |  1 +
 config/routes.rb                              |  2 +
 db/seeds.rb                                   | 10 ++-
 15 files changed, 228 insertions(+), 4 deletions(-)
 create mode 100644 app/controllers/management/institutions_controller.rb
 create mode 100644 app/views/management/institutions/_form.html.erb
 create mode 100644 app/views/management/institutions/edit.html.erb
 create mode 100644 app/views/management/institutions/index.html.erb
 create mode 100644 app/views/management/institutions/index.json.jbuilder
 create mode 100644 app/views/management/institutions/new.html.erb
 create mode 100644 app/views/management/institutions/show.html.erb
 create mode 100644 app/views/management/institutions/show.json.jbuilder

diff --git a/app/controllers/management/institutions_controller.rb b/app/controllers/management/institutions_controller.rb
new file mode 100644
index 000000000..5b21afc9e
--- /dev/null
+++ b/app/controllers/management/institutions_controller.rb
@@ -0,0 +1,74 @@
+class Management::InstitutionsController < ManagementController
+  before_action :set_institution, only: [:show, :edit, :update, :destroy]
+
+  # GET /management/institutions
+  # GET /management/institutions.json
+  def index
+    @institutions = institution_repository.all || []
+  end
+
+  # GET /management/institutions/1
+  # GET /management/institutions/1.json
+  def show
+  end
+
+  # GET /management/institutions/new
+  def new
+    @institution = Institution.new
+  end
+
+  # GET /management/institutions/1/edit
+  def edit
+  end
+
+  # POST /management/institutions
+  # POST /management/institutions.json
+  def create
+    @institution = Institution.new(institution_params)
+
+    respond_to do |format|
+      if institution_repository.create @institution
+        format.html { redirect_to [:management, @institution], notice: 'Institution was successfully created.' }
+        format.json { render :show, status: :created, location: @institution }
+      else
+        format.html { render :new }
+        format.json { render json: @institution.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # PATCH/PUT /management/institutions/1
+  # PATCH/PUT /management/institutions/1.json
+  def update
+    respond_to do |format|
+      if @institution.update(institution_params)
+        format.html { redirect_to [:management, @institution], notice: 'Institution was successfully updated.' }
+        format.json { render :show, status: :ok, location: @institution }
+      else
+        format.html { render :edit }
+        format.json { render json: @institution.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /management/institutions/1
+  # DELETE /management/institutions/1.json
+  def destroy
+    institution_repository.destroy @institution
+    respond_to do |format|
+      format.html { redirect_to management_institutions_url, notice: 'Institution was successfully destroyed.' }
+      format.json { head :no_content }
+    end
+  end
+
+  private
+    # Use callbacks to share common setup or constraints between actions.
+    def set_institution
+      @institution = institution_repository.find(params[:id])
+    end
+
+    # Never trust parameters from the scary internet, only allow the white list through.
+    def institution_params
+      params[:institution]
+    end
+end
diff --git a/app/controllers/management_controller.rb b/app/controllers/management_controller.rb
index 8dc0ebe81..e723f9c42 100644
--- a/app/controllers/management_controller.rb
+++ b/app/controllers/management_controller.rb
@@ -1,7 +1,14 @@
 class ManagementController < ApplicationController
   include RepositoriesProxy
   before_action :authenticate_user!
-
+  before_action :is_admin?
 
   layout 'management'
+
+  private
+    def is_admin?
+      unless current_user.is_admin?
+        redirect_to :root
+      end
+    end
 end
diff --git a/app/models/role.rb b/app/models/role.rb
index 33808ec85..212020038 100644
--- a/app/models/role.rb
+++ b/app/models/role.rb
@@ -3,4 +3,16 @@ class Role < ActiveRecord::Base
 
   validates_presence_of :name
   validates_uniqueness_of :name
+
+  def self.admin
+    self.find_by_name('admin') || self.create!(name: 'admin')
+  end
+
+  def self.teacher
+    self.find_by_name('teacher') || self.create!(name: 'teacher')
+  end
+
+  def self.curator
+    self.find_by_name('student') || self.create!(name: 'student')
+  end
 end
diff --git a/app/models/user.rb b/app/models/user.rb
index 39eecbe80..f34ba02d4 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -50,4 +50,12 @@ class User < ActiveRecord::Base
     learning_object_repository.all self
   end
 
+  def is_admin?
+    roles.each do |role|
+      return true if role.name == "admin"
+    end
+
+    false
+  end
+
 end
diff --git a/app/repositories/orient_db/institution_repository.rb b/app/repositories/orient_db/institution_repository.rb
index 6b74c86ed..ae3eca219 100644
--- a/app/repositories/orient_db/institution_repository.rb
+++ b/app/repositories/orient_db/institution_repository.rb
@@ -2,8 +2,11 @@ module OrientDb
   class InstitutionRepository < Base
     include OrientDb::Methods::EdgeMethods
 
-    def create(name)
-      connection.command sprintf("INSERT INTO Institution (name, created_at, last_modified) VALUES ('%s', '%s', '%s')", name, Time.now, Time.now)
+    def create(institution)
+      institution = connection.command sprintf("INSERT INTO Institution (name, created_at, last_modified, country, city, description, thumbnail, address) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
+        institution.name, Time.now, Time.now, institution.country, institution.city, institution.description, institution.thumbnail, institution.address )
+
+      find(institution["result"][0]["@rid"]) || false
     end
 
     def get_by_name(name)
diff --git a/app/views/management/institutions/_form.html.erb b/app/views/management/institutions/_form.html.erb
new file mode 100644
index 000000000..3c8b6b8a5
--- /dev/null
+++ b/app/views/management/institutions/_form.html.erb
@@ -0,0 +1,55 @@
+<%= form_for([:management, @institution], html: {class: 'ls-form row'}) do |f| %>
+
+    <% if @institution.errors.any? %>
+        <div class="ls-alert-danger">
+          <h2><%= pluralize(@institution.errors.count, "erro") %> <%= "happened".pluralize(@institution.errors.count) %>:</h2>
+          <ul>
+            <% @institution.errors.full_messages.each do |message| %>
+                <li><%= message %></li>
+            <% end %>
+          </ul>
+        </div>
+    <% end %>
+
+    <fieldset>
+      <label class="ls-label col-md-6">
+        <b class="ls-label-text">Nome</b>
+
+        <p>Digite o nome da instituição</p>
+        <%= f.text_field :name, {required: true} %>
+      </label>
+      <label class="ls-label col-md-6">
+        <b class="ls-label-text">Cidade</b>
+
+        <p>Digite a cidade da instituição</p>
+        <%= f.text_field :city %>
+      </label>
+
+      <label class="ls-label col-md-6">
+        <b class="ls-label-text">País</b>
+
+        <p>Digite o país da instituição</p>
+        <%= f.text_field :country %>
+      </label>
+
+      <label class="ls-label col-md-6">
+        <b class="ls-label-text">Endereço</b>
+
+        <p>Digite o endereço da instituição</p>
+        <%= f.text_field :address %>
+      </label>
+
+      <label class="ls-label col-md-6">
+        <b class="ls-label-text">Descrição</b>
+
+        <p>É possível adicionar uma descrição para a insituição</p>
+        <%= f.text_field :description %>
+      </label>
+    </fieldset>
+
+    <div class="ls-actions-btn">
+      <button class="ls-btn">Salvar</button>
+      <%= link_to "Cancelar", management_institutions_path,class: "ls-btn-danger"%>
+    </div>
+
+<% end %>
diff --git a/app/views/management/institutions/edit.html.erb b/app/views/management/institutions/edit.html.erb
new file mode 100644
index 000000000..7590855c1
--- /dev/null
+++ b/app/views/management/institutions/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing Management Institution</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @management_institution %> |
+<%= link_to 'Back', management_institutions_path %>
diff --git a/app/views/management/institutions/index.html.erb b/app/views/management/institutions/index.html.erb
new file mode 100644
index 000000000..b1402beeb
--- /dev/null
+++ b/app/views/management/institutions/index.html.erb
@@ -0,0 +1,31 @@
+<h1 class="ls-title-intro ls-ico-users">Instituições</h1>
+
+<a href="<%= new_management_institution_path %>" class="ls-btn-primary" aria-expanded="false" role="combobox">Cadastrar nova</a>
+
+<table class="ls-table">
+  <thead>
+  <tr>
+    <th>Nome</th>
+    <th>Cidade</th>
+    <th>País</th>
+    <th>Endereço</th>
+    <th>Descrição</th>
+    <!-- <th></th> -->
+    <th></th>
+  </tr>
+  </thead>
+
+  <tbody>
+    <% @institutions.each do |institution| %>
+      <tr>
+        <td><%= institution.name %></td>
+        <td><%= institution.city %></td>
+        <td><%= institution.country %></td>
+        <td><%= institution.address %></td>
+        <td><%= institution.description %></td>
+        <!-- <td class="ls-txt-right ls-regroup"><%= link_to 'Editar', edit_management_institution_path(institution.id) %></td> -->
+        <td><%= link_to 'Excluir', management_institution_path(institution.id), method: :delete, data: { confirm: 'Você tem certeza?' } %></td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
diff --git a/app/views/management/institutions/index.json.jbuilder b/app/views/management/institutions/index.json.jbuilder
new file mode 100644
index 000000000..33501e7e2
--- /dev/null
+++ b/app/views/management/institutions/index.json.jbuilder
@@ -0,0 +1,4 @@
+json.array!(@management_institutions) do |management_institution|
+  json.extract! management_institution, :id
+  json.url management_institution_url(management_institution, format: :json)
+end
diff --git a/app/views/management/institutions/new.html.erb b/app/views/management/institutions/new.html.erb
new file mode 100644
index 000000000..e5100e69a
--- /dev/null
+++ b/app/views/management/institutions/new.html.erb
@@ -0,0 +1,8 @@
+<h1 class="ls-title-intro ls-ico-users">Instituição</h1>
+
+<ol class="ls-breadcrumb">
+  <li><%= link_to 'Instituição', management_institutions_path %></li>
+  <li>Nova Instituição</li>
+</ol>
+
+<%= render 'form' %>
diff --git a/app/views/management/institutions/show.html.erb b/app/views/management/institutions/show.html.erb
new file mode 100644
index 000000000..675726e73
--- /dev/null
+++ b/app/views/management/institutions/show.html.erb
@@ -0,0 +1,4 @@
+<p id="notice"><%= notice %></p>
+
+<%= link_to 'Edit', edit_management_institution_path(@management_institution) %> |
+<%= link_to 'Back', management_institutions_path %>
diff --git a/app/views/management/institutions/show.json.jbuilder b/app/views/management/institutions/show.json.jbuilder
new file mode 100644
index 000000000..40682507b
--- /dev/null
+++ b/app/views/management/institutions/show.json.jbuilder
@@ -0,0 +1 @@
+json.extract! @management_institution, :id, :created_at, :updated_at
diff --git a/app/views/shared/management/_nav_menu.html.erb b/app/views/shared/management/_nav_menu.html.erb
index e86d2e3e0..6dc5552a5 100644
--- a/app/views/shared/management/_nav_menu.html.erb
+++ b/app/views/shared/management/_nav_menu.html.erb
@@ -20,6 +20,7 @@
         <li><%= link_to "Usuários", management_users_path, class: 'ls-submenu-item' %></li>
         <li><%= link_to "Curadores", '#', class: 'ls-submenu-item' %></li>
         <li><%= link_to "Administradores", '#', class: 'ls-submenu-item' %></li>
+        <li><%= link_to "Instituições", management_institutions_path, class: 'ls-submenu-item' %></li>
       </ul>
     </li>
     <li><%= link_to "Denúncias", '#', class: 'ls-ico-bullhorn' %>
diff --git a/config/routes.rb b/config/routes.rb
index de116f148..ba9dbb8d9 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -10,6 +10,8 @@ Rails.application.routes.draw do
   namespace :management do
     root 'welcome#index'
 
+    resources :institutions
+
     resources :statistics do
       collection do
         get :users
diff --git a/db/seeds.rb b/db/seeds.rb
index 1c2b4f8c5..658adbf35 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -7,4 +7,12 @@
 #   Mayor.create(name: 'Emanuel', city: cities.first)
 Role.create(name: 'teacher', description: 'This role represents a Teacher in Portal MEC.')
 Role.create(name: 'admin', description: 'This role represents an MEC Admin, that can perform any action.')
-Role.create(name: 'curator', description: 'This role represents a content Curator in Portal MEC.')
\ No newline at end of file
+Role.create(name: 'curator', description: 'This role represents a content Curator in Portal MEC.')
+
+# create the default admin
+User.create!(
+  email: 'admin@inf.ufpr.br',
+  name: 'admin',
+  password: '123mudar',
+  roles: [Role.admin]
+)
-- 
GitLab