Skip to content
Snippets Groups Projects
Commit 73c9ba33 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

Merge branch 'collections_controller' into 'master'

Add controller, model, views and repository to collections



See merge request !28
parents 51d59521 0b9a4081
No related branches found
No related tags found
No related merge requests found
class CollectionsController < ApplicationController
before_action :set_collection, only: [:show, :edit, :update, :destroy, :like]
# GET /collections
# GET /collections.json
def index
@collections = collection_repository.all
end
# GET /collections/1
# GET /collections/1.json
def show
end
# GET /collections/new
def new
@collection = Collection.new
end
# GET /collections/1/edit
def edit
end
# POST /collections
# POST /collections.json
def create
@collection = Collection.new(collection_params)
respond_to do |format|
if collection_repository.save @collection
format.html { redirect_to @collection, notice: 'Collection was successfully created.' }
else
format.html { render :new }
end
end
end
# PATCH/PUT /collections/1
# PATCH/PUT /collections/1.json
def update
respond_to do |format|
if collection_repository.update(collection_params)
format.html { redirect_to @collection, notice: 'Collection was successfully updated.' }
else
format.html { render :edit }
end
end
end
# DELETE /collections/1
# DELETE /collections/1.json
def destroy
collection_repository.destroy @collection
respond_to do |format|
format.html { redirect_to collections_url, notice: 'Learning object was successfully destroyed.' }
end
end
private
def set_collection
@collection = collection_repository.find("##{params[:id]}")
end
def collection_repository
repository.for(:collection)
end
# Never trust parameters from the scary internet, only allow the white list through.
def collection_params
params[:collection_object]
end
end
class Collection
include ActiveModel::Model
attr_accessor :id, :dateCreation, :description, :highlights, :name, :thumbnail, :views, :likes
end
module OrientDb
class CollectionRepository < Base
include OrientDb::Methods::EdgeMethods
# Example:
# list = repository.for(:collections).all
# list.each do |collection|
# collection_object.inspect <LearningObject model>
# end
def all
hash = connection.query "SELECT FROM Collection"
collections = build_collections(hash)
end
# Usage:
# collection = repository.for(:collections).get_by_dspace_id 123
#
def find(id)
result = connection.query "SELECT FROM #{id}"
build_collection result.first
end
def create(name, url)
connection.command sprintf("INSERT INTO Collection (name) VALUES ('%s')", name)
end
# Usage:
# repository.for(:collections).destroy collection
#
def destroy(collection)
connection.command sprintf("DELETE VERTEX %s", collection.id)
end
private
def build_collection(args={})
return Collection.new(
:id => args["@rid"],
:name => args["name"]
)
end
def build_collections(hash=[])
collections = []
hash.each do |h|
collections << build_collection(h)
end
return collections
end
end
end
<%= form_for(@collection) do |f| %>
<% if @collection.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@collection.errors.count, "error") %> prohibited this learning_object from being saved:</h2>
<ul>
<% @collection.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h1>Editing Institution</h1>
<%= render 'form' %>
<%= link_to 'Show', @institution %> |
<%= link_to 'Back', institutions_path %>
<h1>Listing Institutions</h1>
<% @collections.each do |collection| %>
<%= render 'shared/application/object_vertical', object: collection %>
<% end %>
json.array!(@collections) do |collection|
json.extract! collection, :id, :name
#json.url collection_url(collection, format: :json)
end
<h1>New Institution</h1>
<%= render 'form' %>
<%= link_to 'Back', institutions_path %>
<h1><%= @collection.name %></h1>
json.extract! @collection, :id, :name
......@@ -27,6 +27,7 @@ Repository::Environments.create :development do |repository|
repository.register :carousel, OrientDb::CarouselRepository.new(OrientDb::Client.instance)
repository.register :subject, OrientDb::SubjectRepository.new(OrientDb::Client.instance)
repository.register :institution, OrientDb::InstitutionRepository.new(OrientDb::Client.instance)
repository.register :collection, OrientDb::CollectionRepository.new(OrientDb::Client.instance)
end
Repository::Environments.create :test do |repository|
......@@ -36,6 +37,7 @@ Repository::Environments.create :test do |repository|
repository.register :carousel, OrientDb::CarouselRepository.new(OrientDb::Client.instance)
repository.register :user, UserRepositoryProxy.new(OrientDb::UserRepository.new(OrientDb::Client.instance))
repository.register :subject, OrientDb::SubjectRepository.new(OrientDb::Client.instance)
repository.register :collection, OrientDb::CollectionRepository.new(OrientDb::Client.instance)
end
Repository::Environments.create :production do |repository|
......@@ -45,4 +47,5 @@ Repository::Environments.create :production do |repository|
repository.register :carousel, OrientDb::CarouselRepository.new(OrientDb::Client.instance)
repository.register :user, UserRepositoryProxy.new(OrientDb::UserRepository.new(OrientDb::Client.instance))
repository.register :subject, OrientDb::SubjectRepository.new(OrientDb::Client.instance)
repository.register :collection, OrientDb::CollectionRepository.new(OrientDb::Client.instance)
end
......@@ -25,7 +25,7 @@ Rails.application.routes.draw do
post '/learning_objects/:id/like' => 'learning_objects#like', as: 'like_learning_object'
resources :institutions
resources :collections
get '/faq' => 'welcome#faq'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment