Skip to content

Commit

Permalink
Added room adverts (#393)
Browse files Browse the repository at this point in the history
* added support for rooms

* changed rooms to room adverts

* remove one room advert policy rule

* minor change

* making public room adverts visible to public website

* more small fixes
  • Loading branch information
Ellen-Wittingen authored Mar 2, 2024
1 parent 27772be commit 3e13408
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 131 deletions.
3 changes: 3 additions & 0 deletions app/controllers/v1/room_adverts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class V1::RoomAdvertsController < V1::ApplicationController
before_action :doorkeeper_authorize!, except: %i[index show]
end
12 changes: 12 additions & 0 deletions app/models/room_advert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class RoomAdvert < ApplicationRecord
mount_base64_uploader :cover_photo, CoverPhotoUploader

belongs_to :author, class_name: 'User'

validates :house_name, presence: true
validates :contact, presence: true
validates :description, presence: true
validates :publicly_visible, inclusion: [true, false]

scope :publicly_visible, (-> { where(publicly_visible: true) })
end
33 changes: 33 additions & 0 deletions app/policies/room_advert_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class RoomAdvertPolicy < ApplicationPolicy
class Scope < ApplicationPolicy::Scope
def resolve
if user_can_read?
scope
else
scope.publicly_visible
end
end
end

def index?
true
end

def show?
super || scope.exists?(id: record.id)
end

def update?
user_is_owner? || super
end

def destroy?
user_is_owner? || super
end

private

def user_is_owner?
record.author == user
end
end
31 changes: 31 additions & 0 deletions app/resources/v1/room_advert_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class V1::RoomAdvertResource < V1::ApplicationResource
attributes :house_name, :contact, :location, :available_from,
:description, :description_camofied, :author_name,
:cover_photo_url, :cover_photo, :publicly_visible

def cover_photo_url
@model.cover_photo.url
end

def description_camofied
camofy(@model['description'])
end

def author_name
@model.author.full_name
end

has_one :author, always_include_linkage_data: true

def fetchable_fields
super - [:cover_photo]
end

def self.creatable_fields(_context)
%i[house_name contact location available_from description cover_photo publicly_visible]
end

before_create do
@model.author_id = current_user.id
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
jsonapi_resources :photo_comments
jsonapi_resources :photos, only: %i[index show destroy]
jsonapi_resources :polls
jsonapi_resources :room_adverts
jsonapi_resources :static_pages
jsonapi_resources :stored_mails, only: %i[index show destroy] do
jsonapi_relationships
Expand Down
22 changes: 22 additions & 0 deletions db/migrate/20230727114709_create_room_adverts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateRoomAdverts < ActiveRecord::Migration[7.0]
def change
create_table :room_adverts do |t|
t.string :house_name, null: false
t.string :contact, null: false
t.string :location
t.string :available_from
t.string :description, null: false
t.string :cover_photo
t.boolean :publicly_visible
t.integer :author_id
t.datetime :deleted_at

t.timestamps
end

Permission.create(name: 'room_advert.create')
Permission.create(name: 'room_advert.read')
Permission.create(name: 'room_advert.update')
Permission.create(name: 'room_advert.destroy')
end
end
Loading

0 comments on commit 3e13408

Please sign in to comment.