Skip to content

Commit

Permalink
Merge branch 'main' into 1417-opening-times-stimulus-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaaargZombies committed Aug 30, 2022
2 parents acfe7ad + bebf7b9 commit 921820f
Show file tree
Hide file tree
Showing 22 changed files with 386 additions and 393 deletions.
11 changes: 8 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
GOOGLE_API_KEY=
FACEBOOK_APP_ID=
FACEBOOK_APP_SECRET=
REMOTE_DATABASE_URL=

# A mapbox token is required for the map ui element on events
# You can get one by following the documentation here: https://docs.mapbox.com/api/accounts/tokens/
MAPBOX_TOKEN=

# If you set up postgres via Docker as per the README, you should set these
POSTGRES_HOST=localhost
POSTGRES_USER=postgres
PGPASSWORD=foobar
57 changes: 49 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To run PlaceCal locally you will need:
- A Mac or a Linux machine (we don't support Windows at present)
- Postgres relational database. We are currently using v14.
- Server
- either installed for your distribution or as a docker image (with the correct open port)
- either installed for your distribution or as a docker image (with the correct open port -- see below)
- Client
- you will still need the local developer libraries for postgres
- these are distribution specific so you need to find out what they are called to install them
Expand All @@ -34,6 +34,30 @@ To run PlaceCal locally you will need:

With that said, here's what you need to get rolling.

### Set up postgresql server (Via docker)

**Note: Skip this step if you're using a system-installed docker**

Creating a postgres docker image is reasonably quick:

``` sh
docker network create placecal-network
docker create --name placecal-db --network placecal-network --network-alias postgres -p 5432:5432 --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 -e 'POSTGRES_DB=placecal_db' -e 'POSTGRES_USER=postgres' -e 'POSTGRES_PASSWORD=foobar' -e 'POSTGRES_PORT=5432' postgres:14.1
docker start placecal-db
```

Make a copy of `.env.example` in `.env` in the root directory of the application. These will be loaded in as environment variables when you start the development server.

You can now set the following in `.env`:

``` sh
POSTGRES_HOST=localhost
POSTGRES_USER=postgres
PGPASSWORD=foobar
```

### Set up the local placecal repository, set up the database, and run it

```
git clone https://github.com/geeksforsocialchange/PlaceCal.git
bundle && yarn
Expand All @@ -44,21 +68,38 @@ bundle exec rails import:all_events

* Start the server with `./bin/dev` instead of `bundle exec rails server` due to migration to jsbundling
* Make sure you use `lvh.me:3000` instead of `localhost` or you **will** have authentication problems.
* Admin interface is `admin.lvh.me:3000`
* Seeded root user is [email protected] / password
* Admin interface is `admin.lvh.me:3000` (You will need to make a new admin user -- see below)
* Access code docs through your local filesystem, and update with `bundle exec rails yard`

To set up your own server, take a look at `INSTALL.md`.

### Creating an admin user

To create an admin user, open the console (`bin/rails c`) and run the following:

```
User.create!(email: '[email protected]', password: 'password', password_confirmation: 'password', role: :root)
```

(Note: You should replace '[email protected]' and 'password' with more appropriate values)

## Testing

All PlaceCal tests are written in minitest. Before running tests make sure you dev environment has all the migrations run and then run `rails db:test:prepare` which will load the schema into the test database.
PlaceCal tests are written in minitest. Before running the tests please ensure your dev environment has all of the migrations run, and ensure you have loaded the schema into the test database by running:

``` sh
rails db:test:prepare
```

Running unit tests is as simple as `rails test` for all tests, `rails test test/models/user.rb` to run every test in one test file and `rails test test/models/user.rb:123` to run only ONE test.
The following commands are used for running tests:

``` sh
rails test # To run all of the unit tests
rails test:system # To run all of the system tests (Invokes a headless browser)
rails test:all # To run both the unit tests and the system tests at once
```

System tests are where the application is started and connected to a 'headless' browser that is then used to verify functionality. This is employed as it allows us to verify our javascript is behaving.
When running the unit tests these systems tests are NOT run as they can take a lot of time and use a large amount of RAM.
To run system tests invoke `rails test:system`. It has the same options as the unit tests above.
Please note that when running unit tests, system tests are **not** run, this is because they can take a while to run and are quite resource intensive. To perform more advanced usage like executing only a specific test or test file, see the documentation [here](https://guides.rubyonrails.org/testing.html)

When pushing to a branch on github all tests are run (unit and system). This is configured [here](.github/workflows/test.yml). You are not allowed to merge a branch (onto main or production) without a passing test suite.

Expand Down
1 change: 0 additions & 1 deletion app/components/admin_edit/_admin_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
<h1 class="page-header"><%= title %></h1>

<div class="form">
<div class="behavior-trigger" data-behavior="<%= properties[:model] %>.form"></div>
<%= render 'form' %>
</div>
1 change: 0 additions & 1 deletion app/components/admin_new/_admin_new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<h1 class="page-header">Create a new <%= properties[:model].to_s.humanize %></h1>

<div class="form">
<div class="behavior-trigger" data-behavior="<%= properties[:model] %>.form"></div>
<%= render 'form' %>
</div>
4 changes: 0 additions & 4 deletions app/javascript/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ import Vue from "vue"
import "vue-turbolinks"

// Specific pages
import "./src/behaviors/all_behaviors.js"
import "./src/calendar-form.js"
import "./src/ward-picker.js"

$(document).on("turbo:load", function () {

$("body").init_behaviors()

$("[data-toggle='tooltip']").tooltip()
});

Expand Down
4 changes: 0 additions & 4 deletions app/javascript/src/behaviors/all_behaviors.js

This file was deleted.

37 changes: 0 additions & 37 deletions app/javascript/src/behaviors/behaviors.base.js

This file was deleted.

11 changes: 0 additions & 11 deletions app/javascript/src/behaviors/behaviors.collection.js

This file was deleted.

43 changes: 0 additions & 43 deletions app/javascript/src/behaviors/behaviors.map.js

This file was deleted.

39 changes: 0 additions & 39 deletions app/javascript/src/behaviors/behaviors.partner.js

This file was deleted.

49 changes: 0 additions & 49 deletions app/javascript/src/behaviors/behaviors.place.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/neighbourhood.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def name_abbr=(value)

self['name_abbr'] = value.present? ? value : nil
end

def to_s
"#{fullname} (#{unit})"
end
Expand Down
36 changes: 31 additions & 5 deletions app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,50 @@ class Partner < ApplicationRecord
}

# Takes in a Site and fetches all Partners for that site
# If the site has tags, the list of partners will be filtered by those tags
# In its basic mode (without tags) it looks for partners by address
# or service area and returns a distinct set (as a partner can
# have many service areas or an address that overlaps).
# If the site has tags present then the filter is constrained to
# only allow partners that have had that tag applied (and then the
# same rule applies as above).
# If no site tags exist then skip that part of the query. If no
# site neighbourhoods exist then return an empty scope
#
# @param site [Site] The site we want partners for.
# @return [ActiveRecord::Relation<Partner>]
scope :for_site, lambda { |site|
site_neighbourhood_ids = site.owned_neighbourhoods.map(&:id)
query = Partner

# if site has tags show only partners WITH those tags
site_tag_ids = site.tags.map(&:id)
partners = from_neighbourhoods_and_service_areas(site_neighbourhood_ids)
if site_tag_ids.any?
query = query
.left_joins(:partner_tags)
.where("partner_tags.tag_id in (?)", site_tag_ids)
end

# now look for addresses and service areas
site_neighbourhood_ids = site.owned_neighbourhoods.map(&:id)

# skip everything if site has no neighbourhoods
return none if site_neighbourhood_ids.empty?

site_tag_ids.any? ? partners.with_tags(site_tag_ids) : partners
query
.left_joins(:address, :service_areas)
.where(
'(service_areas.neighbourhood_id in (:neighbourhood_ids) OR addresses.neighbourhood_id in (:neighbourhood_ids))',
neighbourhood_ids: site_neighbourhood_ids)
.distinct
}

# Get a list of Partners that have the given tags
#
# @param tags [Array<Tag>] A list of tags
# @return [ActiveRecord::Relation<Partner>]
scope :with_tags, ->(tags) { joins(:partner_tags).where(partner_tags: { tag: tags }) }
scope :with_tags, ->(tag_ids) {
left_joins(:partner_tags)
.where("partner_tags.tag_id in (?)", tag_ids)
}

# only select partners that have addresses
scope :with_address, -> do
Expand Down
2 changes: 0 additions & 2 deletions app/views/admin/partners/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<div class="form">
<div class="behavior-trigger" data-behavior="<%= model.to_s.downcase %>.form"></div>

<%= simple_form_for @partner do |f| %>

<%= render_component "error", object: @partner %>
Expand Down
2 changes: 0 additions & 2 deletions app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<h1 class="page-header">Edit User: <%= @user.full_name %></h1>

<div class="behavior-trigger" data-behavior="user.form"></div>

<%- disabled_fields = policy(@user).disabled_attributes_for_update %>
<%- displayable_fields = policy(@user).permitted_attributes_for_update %>

Expand Down
Loading

0 comments on commit 921820f

Please sign in to comment.