### Install PostgreSQL Client (Bash)
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
Installs the PostgreSQL client and contrib packages on an Ubuntu system, allowing connection to PostgreSQL servers.
```bash
sudo apt update
sudo apt install postgresql postgresql-contrib
```
--------------------------------
### Build and Run Docker Compose Services
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Builds the Docker images defined in docker-compose.yml and starts the services. It also includes commands to create and migrate the database for both the application and test environments, and run specific tests.
```bash
docker-compose build
docker-compose up
docker-compose run --rm app rails db:create
docker-compose run --rm test rails db:create
docker-compose run --rm app rails db:migrate
docker-compose run --rm test rails db:migrate
# make sure docker-compose up is working and run this test:
docker-compose run --rm test rails test test/controllers/admin/sidekiq_controller_test.rb
```
--------------------------------
### Build and Run Docker Project
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Builds the Docker images for the project using docker-compose and then starts the application services. The `docker attach` command connects to the main application container's logs.
```bash
docker-compose build
docker-compose up
docker attach solutions_app
```
--------------------------------
### Clone and Navigate Project Directory
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Clones the violet_rails project from GitHub and navigates into the project directory. This is the initial step to get the project code onto your local machine.
```bash
git clone git@github.com:restarone/violet_rails.git
cd violet_rails
```
--------------------------------
### Database Migration and Initial Setup for Violet Rails
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-Heroku
This section details the commands to migrate the database on Heroku and access the Heroku console to run generators for setting up the root subdomain and landing page. This is crucial for initial application functionality and user management.
```bash
heroku run rake db:migrate
heroku console
```
--------------------------------
### Clone Violet Rails Repository
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Clones the Violet Rails project from the specified Git repository URL. This is the initial step for using the template as a starting point for a new application.
```bash
git clone git@github.com:restarone/violet_rails.git
```
--------------------------------
### Install mobile_client_configuration v1 Plugin in Violet Rails
Source: https://github.com/restarone/violet_rails/wiki/Defining-a-Mobile-Client-Configuration-(iOS)
This script installs the 'mobile_client_configuration v1' plugin into a specified schema within the Violet Web Console. Ensure you replace 'schema' with your target subdomain. This action creates a new API namespace for the mobile client configuration.
```ruby
Apartment::Tenant.switch('schema') { ApiNamespace.create!(name: 'mobile_client_configuration',slug: 'mobile_client_configuration',version: 1,requires_authentication: false,namespace_type: 'create-read-update-delete',properties: { tab_navigation: [ { path: "/", label: "Home" } ] }) }
```
--------------------------------
### Prepare and Run Test Database
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Commands to create and migrate the test database before running tests. This ensures the test environment is set up correctly.
```bash
docker-compose run --rm solutions_test rails db:create
docker-compose run --rm solutions_test rails db:migrate
```
--------------------------------
### Install zlib for rbenv OpenSSL (Bash)
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
Installs the zlib development library, which may be required to resolve errors when installing OpenSSL for rbenv.
```bash
sudo apt-get install libz-dev
```
--------------------------------
### Install App Dependencies on EC2 (Bash)
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
Installs necessary system packages, Node.js, npm, rbenv, Ruby, Bundler, Rails, Yarn, and Redis on an Ubuntu EC2 instance. It also configures rbenv and installs Nginx.
```bash
sudo apt-get update -y && sudo apt-get upgrade -y
sudo apt install curl -y
sudo apt install nodejs -y && sudo apt install npm -y
sudo apt-get install htop imagemagick build-essential zlib1g-dev openssl libreadline6-dev git-core zlib1g libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf automake libtool bison libgecode-dev -y && sudo apt-get install libpq-dev -y
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc && echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv && git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
source ~/.bashrc
rbenv install 2.6.6 && rbenv global 2.6.6 && echo 'gem: --no-ri --no-rdoc' >> ~/.gemrc && source ~/.bashrc && gem install bundler:2.1.4 && gem install rails -v 6.1.3
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update -y
sudo apt install yarn -y
sudo apt install redis-server -y
sudo apt-get install nginx -y
```
--------------------------------
### Access Application and Admin Interface
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Provides the URLs to access the running application and its admin interfaces. It specifies credentials for logging into the admin section.
```bash
visit `localhost:80`, `lvh.me:5250` or `localhost:5250`
To Login, visit `lvh.me:5250/admin` use the following credentials (created by seeds.rb)
user: `violet@rails.com`
password: `123456`
```
--------------------------------
### Precompile Assets
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Precompiles the application's assets, such as JavaScript and CSS, for production use. This is a one-time step and can take a significant amount of time (5-20 minutes).
```bash
docker-compose run --rm solutions_app rails assets:precompile
```
--------------------------------
### Configure and Manage Sidekiq Systemd Service
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
This snippet shows how to configure the Sidekiq systemd service file, including user, working directory, environment variables, and startup/shutdown commands. It also includes commands to reload the daemon, enable, start, and check the status of the Sidekiq service. Viewing logs is done using 'journalctl'.
```bash
[Unit]
Description = sidekiq
After = network.target
[Service]
Type = simple
User = ubuntu
WorkingDirectory = /var/www/violet/current
EnvironmentFile = /var/www/violet/.rbenv-vars
ExecStart = /home/ubuntu/.rbenv/bin/rbenv exec bundle exec sidekiq -C config/sidekiq.yml
ExecStop = ps -ef | grep sidekiq | grep -v grep | awk '{print $2}' | xargs kill -9
TimeoutSec = 15
Restart = always
[Install]
WantedBy = multi-user.target
```
```bash
sudo systemctl daemon-reload
sudo systemctl enable sidekiq.service
sudo systemctl start sidekiq.service
sudo systemctl status sidekiq.service
```
```bash
journalctl -u sidekiq.service -f
```
--------------------------------
### Attach to Sidekiq Container for Debugging
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Attaches to the `solutions_sidekiq` Docker container to inspect Sidekiq logs or set breakpoints for debugging background job processing.
```bash
docker attach solutions_sidekiq
```
--------------------------------
### Search Field Helper and Handler
Source: https://github.com/restarone/violet_rails/wiki/custom-web-development-guide
Provides a helper to render a search field and a JavaScript example for handling search form submissions via AJAX.
```APIDOC
## Searching website pages
### Description
This section details how to render a search field using a helper and handle search queries via AJAX.
### Method
POST
### Endpoint
`/query` (for direct AJAX calls)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **query** (string) - Required - The search term.
### Request Example (Direct AJAX)
```javascript
$.ajax({
type: 'POST',
url: '/query',
data: { query: "test" },
dataType: 'json',
success(data) {
console.log(data);
return false;
},
error(data) {
console.log(data);
}
})
```
### Response
#### Success Response (200)
- **search_results** (object) - Contains the search results, potentially including preview content.
#### Response Example
(Response structure depends on how search results are formatted, but will contain preview content if configured)
```json
{
"preview_content": "HTML markup for search results"
}
```
### Styling Search Results
Each page has a preview content form. Enter your HTML markup there and the API will return it in the response.
```
--------------------------------
### Run Rails Console
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Opens an interactive Rails console session within the solutions_app Docker container, allowing direct interaction with the application's models and logic.
```bash
docker-compose run --rm solutions_app rails c
```
--------------------------------
### Run Full Test Suite
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Executes the complete test suite for the application. This can be done using a custom script or the standard Rails test runner within Docker.
```bash
./clean_run_tests.sh
# or
docker-compose run --rm solutions_test rails test
# or
./run_tests.sh
```
--------------------------------
### Display Encrypted Private Key
Source: https://github.com/restarone/violet_rails/wiki/Direct‐to‐customer-deployment-using-a-Github-Action
Displays the content of the encrypted private key file, which should be copied to the DEPLOY_ID_RSA_ENC GitHub secret.
```bash
cat deploy_id_rsa_enc
```
--------------------------------
### Initial Deployment with Capistrano (Bash)
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
Executes the Capistrano deployment script for the production environment. This step is expected to fail but sets up the necessary file structure.
```bash
bundle exec cap production deploy
```
--------------------------------
### Connect to PostgreSQL Database
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Connects to the specified PostgreSQL database instance using the provided host, port, database name, and username. This is often a prerequisite for performing database maintenance tasks.
```bash
psql -h ec2-3-83-199-128.compute-1.amazonaws.com -p 5432 -d postgres -U postgres
```
--------------------------------
### Display Public SSH Key
Source: https://github.com/restarone/violet_rails/wiki/Direct‐to‐customer-deployment-using-a-Github-Action
Prints the content of the public SSH key to the console, which is then used for adding to GitHub deployment keys.
```bash
cat ~/.ssh/id_rsa.pub
```
--------------------------------
### Attach to Application Container for Debugging
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Attaches to the `solutions_app` Docker container to view live logs and enable debugging tools like `byebug` or `pry` when breakpoints are hit.
```bash
docker attach solutions_app
```
--------------------------------
### Deploy to Staging Environment using Capistrano
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Deploys the application to the staging environment. The BRANCH environment variable specifies which branch to deploy.
```bash
BRANCH=branch bundle exec cap staging deploy
```
--------------------------------
### Create PostgreSQL User and Database (SQL/Bash)
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
SQL commands to create a new PostgreSQL role, set a password, and create a database for the application. The bash command then creates the production database.
```sql
CREATE ROLE username;
ALTER ROLE username WITH LOGIN;
ALTER USER username CREATEDB;
ALTER USER username WITH PASSWORD 'passwordhere';
\q
```
```bash
psql -h endpoint.amazonaws.com -p 5432 -U username -d postgres -c 'CREATE DATABASE violet_production'
```
--------------------------------
### Clean Up Docker Application Container
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Stops and removes the `solutions_app` Docker container, rebuilds the image, and restarts the container. This is useful for cleaning up stale application states and resolving issues related to the container.
```bash
docker-compose stop solutions_app \
&& docker-compose rm -f solutions_app \
&& docker-compose build \
&& docker-compose up -d solutions_app
```
--------------------------------
### Export Public SSH Key
Source: https://github.com/restarone/violet_rails/wiki/Direct‐to‐customer-deployment-using-a-Github-Action
Appends the generated public SSH key to the authorized_keys file, allowing access using this key.
```bash
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
```
--------------------------------
### Create Application Configuration Files (Bash)
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
Creates empty configuration files in the shared config directory for the Rails application. These files will be populated with specific environment settings.
```bash
cd /var/www/violet/shared/config
touch cable.yml database.yml secrets.yml storage.yml webpacker.yml sidekiq.yml
```
--------------------------------
### Run Single Test
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Executes a specific test file or test case within the Rails test suite. Replace `path/to/test` with the actual path to the test file.
```bash
docker-compose run --rm solutions_test rails test path/to/test
```
--------------------------------
### Access Subdomain and Domain Admin
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Details how to access the subdomain admin and the global domain admin interfaces. Subdomain admins manage specific subdomains, while domain admins have broader control.
```bash
to access the subdomain admin, visit `lvh.me:5250/admin` or `subdomain.lvh.me:5250/admin`
to access domain admin visit `lvh.me:5250/sysadmin`
```
--------------------------------
### Set Development Environment Variables
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Defines development-specific environment variables by creating a `.env.development` file in the project root. These variables are automatically used when running within Docker.
```bash
RAILS_ENV=development
DATABASE_HOST=solutions_db
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=password
DATABASE_NAME=r_solutions_development
DATABASE_PORT=5432
APP_HOST=localhost
REDIS_URL=redis://solutions_redis:6379/12
RACK_TIMEOUT_SERVICE_TIMEOUT=200
```
--------------------------------
### Configure Local PostgreSQL Database for Rails
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Temporarily replaces the database.yml configuration to point to a local PostgreSQL database. This is necessary when running parts of the application outside of Docker, such as with Capistrano deployments.
```yaml
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
host: localhost
port: 5432
database: solutions_development
username: postgres
password: postgres
test:
<<: *default
host: localhost
port: 5432
database: solutions_development
username: postgres
password: postgres
staging:
<<: *default
host: localhost
port: 5432
database: solutions_development
username: postgres
password: postgres
production:
<<: *default
host: localhost
port: 5432
database: solutions_development
username: postgres
password: postgres
```
--------------------------------
### Create, Migrate, and Seed Database
Source: https://github.com/restarone/violet_rails/wiki/Getting-started-(development-cheatsheet)
Executes commands within the solutions_app Docker container to create the database, run migrations, and populate it with seed data. The `db/seeds.rb` file contains the data that will be seeded.
```bash
docker-compose run --rm solutions_app rails db:create
docker-compose run --rm solutions_app rails db:migrate
docker-compose run --rm solutions_app rails db:seed
```
--------------------------------
### Install rbenv-vars Plugin (Bash)
Source: https://github.com/restarone/violet_rails/wiki/Deploying-to-EC2-(with-Capistrano)
Clones the rbenv-vars plugin into the rbenv plugins directory, which allows managing environment variables for Ruby applications.
```bash
git clone https://github.com/rbenv/rbenv-vars.git $(rbenv root)/plugins/rbenv-vars
```
--------------------------------
### CSS for Video Element Opacity Transition
Source: https://github.com/restarone/violet_rails/wiki/custom-web-development-guide
This CSS rule sets the initial opacity of the video element to 0 and defines a transition effect for when the opacity changes. This allows for a smooth fade-in effect when the video starts playing.
```css
#video {
opacity: 0;
transition: opacity 2s ease;
}
```
--------------------------------
### Linking Images with CMS File Link
Source: https://github.com/restarone/violet_rails/wiki/custom-web-development-guide
This example shows how to embed an image from the CMS into your HTML content using the `cms:file_link` helper. It specifies the file ID, desired output format ('image'), and CSS classes. The output is a standard `` tag with a dynamically generated `src` attribute.
```html