diff --git a/app/controllers/v1/collections_controller.rb b/app/controllers/v1/collections_controller.rb index b203f6b3164caa7a8eaf3a2cce6f8e2f6cb8c7e5..b0dea3d5722d5ad0ecc3e0e839f3b85bbc7c9243 100644 --- a/app/controllers/v1/collections_controller.rb +++ b/app/controllers/v1/collections_controller.rb @@ -16,7 +16,7 @@ class V1::CollectionsController < ApplicationController # GET /v1/collections # GET /v1/collections.json def index - collections = paginate Collection + collections = paginate policy_scope(Collection) render json: collections end diff --git a/app/controllers/v1/contacts_controller.rb b/app/controllers/v1/contacts_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..d1e589b425c6a77f931d9036f71b966822524c5d --- /dev/null +++ b/app/controllers/v1/contacts_controller.rb @@ -0,0 +1,52 @@ +class V1::ContactsController < ApplicationController + include ::Paginator + + before_action :set_contact, only: [:show, :update, :destroy] + + # GET v1/contacts + def index + contacts = paginate Contact + render json: contacts + end + + # GET v1/contacts/1 + def show + render json: @contact + end + + # POST v1/contacts + def create + @contact = Contact.new(contact_params) + if @contact.save + render json: @contact, status: :created + else + render json: @contact.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT v1/contacts/1 + def update + if @contact.update(contact_params) + render json: @contact + else + render json: @contact.errors, status: :unprocessable_entity + end + end + + # DELETE v1/contacts/1 + def destroy + @contact.destroy + render status: :ok + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_contact + @contact = Contact.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def contact_params + params.require(:contact).permit(:name, :email, :message) + end +end diff --git a/app/controllers/v1/omniauth_callbacks_controller.rb b/app/controllers/v1/omniauth_callbacks_controller.rb index a2edc1e4a769bf2deba4bf3fa2658d0e0ab4f5fa..54854b7cfb3ece484680ee764259ea0d5d7e13cd 100644 --- a/app/controllers/v1/omniauth_callbacks_controller.rb +++ b/app/controllers/v1/omniauth_callbacks_controller.rb @@ -1,3 +1,4 @@ +require 'open-uri' class V1::OmniauthCallbacksController < DeviseTokenAuth::ApplicationController attr_reader :auth_params @@ -76,8 +77,7 @@ # break out provider attribute assignment for easy method extension def assign_provider_attrs(user, auth_hash) - avatar = auth_hash['provider']=='google_oauth2' ? "" : auth_hash['info']['image'] - + avatar = auth_hash['provider']=='google_oauth2' ? open(auth_hash['info']['image']) : auth_hash['info']['image'] user.assign_attributes({ nickname: auth_hash['info']['nickname'], diff --git a/app/controllers/v1/suggestions_controller.rb b/app/controllers/v1/suggestions_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..b12c6d7078f53dfdf68a69ea4b47a5a0c150cee8 --- /dev/null +++ b/app/controllers/v1/suggestions_controller.rb @@ -0,0 +1,53 @@ +class V1::SuggestionsController < ApplicationController + include ::Paginator + + before_action :set_suggestion, only: [:show, :update, :destroy] + + # GET v1/suggestions + def index + suggestions= paginate Suggestion + render json: suggestions + end + + # GET v1/suggestions/1 + def show + render json: @suggestion + end + + # POST v1/suggestions + def create + @suggestion = Suggestion.new(suggestion_params) + + if @suggestion.save + render json: @suggestion, status: :created + else + render json: @suggestion.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT v1/suggestions/1 + def update + if @suggestion.update(suggestion_params) + render json: @suggestion + else + render json: @suggestion.errors, status: :unprocessable_entity + end + end + + # DELETE v1/suggestions/1 + def destroy + @suggestion.destroy + render status: :ok + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_suggestion + @suggestion = Suggestion.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def suggestion_params + params.require(:suggestion).permit(:name, :link, :description, :status) + end +end diff --git a/app/models/collection_item.rb b/app/models/collection_item.rb index a412f883bd5608a70d4185d758d3aa4a9f335a16..4fc48636bdb07fefc3468a79dc6dce01bafcb704 100644 --- a/app/models/collection_item.rb +++ b/app/models/collection_item.rb @@ -24,4 +24,9 @@ class CollectionItem < ApplicationRecord def recipient collection end + + def thumbnail + collectionable_type == 'LearningObject' ? LearningObject.find(collectionable_id).default_thumbnail : Collection.find(collectionable_id).thumbnail + end + end diff --git a/app/models/contact.rb b/app/models/contact.rb new file mode 100644 index 0000000000000000000000000000000000000000..4d63e81922f4fcde4827c07dcc45635e15fc7989 --- /dev/null +++ b/app/models/contact.rb @@ -0,0 +1,11 @@ +# == Schema Information +# +# Table name: contacts +# +# id :integer not null, primary key +# name :string +# email :string +# message :text +# +class Contact < ApplicationRecord +end diff --git a/app/models/suggestion.rb b/app/models/suggestion.rb new file mode 100644 index 0000000000000000000000000000000000000000..a528f432672f147c0ca531d750bb49d86e65bae4 --- /dev/null +++ b/app/models/suggestion.rb @@ -0,0 +1,13 @@ +# == Schema Information +# +# Table name: suggestions +# +# id :integer not null, primary key +# name :string +# link :string +# description :text +# status :integer +# +class Suggestion < ApplicationRecord + enum status: { created: 0, accepted: 1, rejected: 2, loaded: 3 } +end diff --git a/app/policies/collection_policy.rb b/app/policies/collection_policy.rb index 8aeafcd2e3301106ac7b09318f47d12ce57b1593..7ca595c7cd867bdbaf48d7f9c858c140a0fe1a21 100644 --- a/app/policies/collection_policy.rb +++ b/app/policies/collection_policy.rb @@ -6,17 +6,11 @@ class CollectionPolicy < ApplicationPolicy include StageablePolicy class Scope < Scope - def initialize(user, user_id, scope) - @user = user - @user_id = user_id - @scope = scope - end - def resolve - if @user.try(:id) == @user_id || @user.try(:is_admin?) + if !user.nil? && user.is_admin? scope.all else - scope.where(privacy: 'public') + scope.includes(:collection_items).where.not(:collection_items => {:collection_id => nil}).where(privacy: 'public') end end end diff --git a/app/serializers/collection_serializer.rb b/app/serializers/collection_serializer.rb index 70672b9d0b941630bdd19b1198acf972c28d1dbf..07298a65e089e61beca40ae3b32a6d2c920863d6 100644 --- a/app/serializers/collection_serializer.rb +++ b/app/serializers/collection_serializer.rb @@ -17,7 +17,19 @@ class CollectionSerializer < ActiveModel::Serializer object.followed? current_user end - attributes :id, :name, :created_at, :updated_at, :description, :privacy, :score, :likes_count, :liked, :reviewed, :complained, :followed, :review_average, :thumbnail + def items_thumbnails + thumbs = [] + i = 0 + while thumbs.size < 4 && i < object.collection_items.size do + t = object.collection_items[i].thumbnail + thumbs << t unless t.blank? + i=i+1 + end + thumbs + end + + attributes :id, :name, :created_at, :updated_at, :description, :privacy, :score, :likes_count, :liked, :reviewed, :complained, :review_average, :thumbnail, :items_thumbnails + belongs_to :owner has_many :tags has_many :subjects diff --git a/app/serializers/contact_serializer.rb b/app/serializers/contact_serializer.rb new file mode 100644 index 0000000000000000000000000000000000000000..b3eca6681df75dec18dc84a302ac7740c0d724f2 --- /dev/null +++ b/app/serializers/contact_serializer.rb @@ -0,0 +1,4 @@ +class ContactSerializer < ActiveModel::Serializer + cache key: 'contact', expires_in: 24.hours + attributes :id, :name, :email, :message +end diff --git a/app/serializers/suggestion_serializer.rb b/app/serializers/suggestion_serializer.rb new file mode 100644 index 0000000000000000000000000000000000000000..d362e9188490676375e1dd6c42fab9cc3d0fe885 --- /dev/null +++ b/app/serializers/suggestion_serializer.rb @@ -0,0 +1,3 @@ +class SuggestionSerializer < ActiveModel::Serializer + attributes :id, :name, :link, :description, :status +end diff --git a/config/routes.rb b/config/routes.rb index c799fa8d47ee6d0fa4e7c88aba4bfcde0bc5ad90..6c5e0d8ee2549c1e1298e5f06a0a81b1a6e2c172 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -139,6 +139,8 @@ Rails.application.routes.draw do resources :roles, except: [:new, :edit] resources :scores, only: [:index, :show, :update] resources :ratings, except: [:new, :edit] + resources :contacts + resources :suggestions post '/package', to: 'packages#link' get '/subjects', to: 'subjects#index' diff --git a/db/migrate/20170207122721_create_contacts.rb b/db/migrate/20170207122721_create_contacts.rb new file mode 100644 index 0000000000000000000000000000000000000000..7c7f4d52e9b2ad186f03fdccd5a630fa54fcb812 --- /dev/null +++ b/db/migrate/20170207122721_create_contacts.rb @@ -0,0 +1,11 @@ +class CreateContacts < ActiveRecord::Migration[5.0] + def change + create_table :contacts do |t| + t.string :name + t.string :email + t.text :message + + t.timestamps + end + end +end diff --git a/db/migrate/20170208111714_create_suggestions.rb b/db/migrate/20170208111714_create_suggestions.rb new file mode 100644 index 0000000000000000000000000000000000000000..c59188a82576b3d6b80f7fad8a5b5ffd25eb330a --- /dev/null +++ b/db/migrate/20170208111714_create_suggestions.rb @@ -0,0 +1,12 @@ +class CreateSuggestions < ActiveRecord::Migration[5.0] + def change + create_table :suggestions do |t| + t.string :name + t.string :link + t.text :description + t.integer :status, default: 0 + + t.timestamps + end + end +end