diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..e9a1359aeec0a3d145828c434b84107be363c4a5 --- /dev/null +++ b/app/controllers/collections_controller.rb @@ -0,0 +1,77 @@ +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 diff --git a/app/models/collection.rb b/app/models/collection.rb new file mode 100644 index 0000000000000000000000000000000000000000..aaa1bc126f029bb960b14ce1bcb15fd0cbd1201c --- /dev/null +++ b/app/models/collection.rb @@ -0,0 +1,5 @@ +class Collection + include ActiveModel::Model + + attr_accessor :id, :dateCreation, :description, :highlights, :name, :thumbnail, :views, :likes +end diff --git a/app/repositories/orient_db/collection_repository.rb b/app/repositories/orient_db/collection_repository.rb new file mode 100644 index 0000000000000000000000000000000000000000..c812590eab75fd06bd36e2f5ca54d15a616a6dba --- /dev/null +++ b/app/repositories/orient_db/collection_repository.rb @@ -0,0 +1,52 @@ +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 diff --git a/app/views/collections/_form.html.erb b/app/views/collections/_form.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..91c6545768704012706b4c7578b3534c7c2a7ef6 --- /dev/null +++ b/app/views/collections/_form.html.erb @@ -0,0 +1,17 @@ +<%= 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 %> diff --git a/app/views/collections/edit.html.erb b/app/views/collections/edit.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..6146cc3a11722b79aac7b51e2467f85d2e406c13 --- /dev/null +++ b/app/views/collections/edit.html.erb @@ -0,0 +1,6 @@ +<h1>Editing Institution</h1> + +<%= render 'form' %> + +<%= link_to 'Show', @institution %> | +<%= link_to 'Back', institutions_path %> diff --git a/app/views/collections/index.html.erb b/app/views/collections/index.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..165184353ba8e33be3b955729452d72e31c82e58 --- /dev/null +++ b/app/views/collections/index.html.erb @@ -0,0 +1,5 @@ +<h1>Listing Institutions</h1> + +<% @collections.each do |collection| %> + <%= render 'shared/application/object_vertical', object: collection %> +<% end %> diff --git a/app/views/collections/index.json.jbuilder b/app/views/collections/index.json.jbuilder new file mode 100644 index 0000000000000000000000000000000000000000..ab3f845e60ad913050aecf50f3ac3032e4ce6c7a --- /dev/null +++ b/app/views/collections/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@collections) do |collection| + json.extract! collection, :id, :name + #json.url collection_url(collection, format: :json) +end diff --git a/app/views/collections/new.html.erb b/app/views/collections/new.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..75bb5679fb942a774c05f839b972258a94ad2610 --- /dev/null +++ b/app/views/collections/new.html.erb @@ -0,0 +1,5 @@ +<h1>New Institution</h1> + +<%= render 'form' %> + +<%= link_to 'Back', institutions_path %> diff --git a/app/views/collections/show.html.erb b/app/views/collections/show.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..c224d3447d983e5f37cb4f4649b8aeb531ff483a --- /dev/null +++ b/app/views/collections/show.html.erb @@ -0,0 +1 @@ +<h1><%= @collection.name %></h1> diff --git a/app/views/collections/show.json.jbuilder b/app/views/collections/show.json.jbuilder new file mode 100644 index 0000000000000000000000000000000000000000..64c29dac93b6771a3fe28d4cb989186a5f058fc8 --- /dev/null +++ b/app/views/collections/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @collection, :id, :name diff --git a/config/initializers/repositories/repositories.rb b/config/initializers/repositories/repositories.rb index 0f9c9cd8fda45a15cd10f4bb88bfd53a81a00262..791a7454458887f766d062eb7ba4e50b2fe03d01 100644 --- a/config/initializers/repositories/repositories.rb +++ b/config/initializers/repositories/repositories.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 9445e45c115e3f6f0c2b0b75bfa5242ae95e0ccb..efd7312c0098bbf9c7a8565e69bd4d1134d2ac41 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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'