A administrative tool for HDFFA staff members to manage their partners, sponsored by the High Desert Food and Farm Alliance.
This is a Rails 5.x app with Ruby ~2.6 and PostgreSQL.
Install RVM globally:
Mac:
-
Confirm you have brew installed by running:
brew --version
It should return something like:Homebrew 2.5.6
-
Go to RVM and follow step by step:
- If the first command to install gnupg and gnupg2 does not work:
-
Run
gpg --version
If it returns something likegpg (GnuPG) 2.2.23
Then everywhere it saysgpg2
, typegpg
instead -
If after you run:
gpg --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
It returns:gpg: keyserver receive failed: No name
- Run
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
- Followed by:
\curl -sSL https://get.rvm.io | bash -s stable
- It should return:
Thanks for installing RVM 🙏
\
- Run
-
Run:
source /Users/<your_username>/.rvm/scripts/rvm
-
Confirm RVM installed:
which rvm
It should return:/Users/<your_username>/.rvm/bin/rvm
-
- If the first command to install gnupg and gnupg2 does not work:
-
RVM is now installed
Install Postgres globally
-
Create a location for the project on your machine
-
Clone this repository by Running:
git clone https://github.com/osu-cascades/hdffa-directory.git
-
cd
into the project directory and runrvm install 2.6.6
to install the correct version of Rails required for this project
After cloning this repository and cd
ing into it, get up and running with:
bundle install
rails db:setup
rails s
There are a few steps to get up and running in development.
RECAPTCHA_SITE_KEY
RECAPTCHA_SECRET_KEY
See .env.example for a complete list of expected environment variables.
This app is using minitest / Rails default tests. Run the suite with:
rails test
Note: There is a Guardfile should you wish to use guard.
There is a staging and production environment hosted by Heroku.
heroku git:remote -a hdffa-directory-staging
git remote rename heroku staging
heroku git:remote -a hdffa-directory
git remote rename heroku production
By renaming the remotes, you can then deploy with
git push staging
git push production
Configure env vars in staging and production:
RECAPTCHA_SITE_KEY
RECAPTCHA_SECRET_KEY
LOCALHOST:
- Tear down/clear out the Database:
rails db:reset
- Recreate the tables:
rails db:migrate
- Import the data:
rails db:import_partners
HEROKU STAGING:
- Tear down/clear out the Database:
heroku pg:reset -rstaging
- Recreate the tables:
heroku run rails db:migrate -rstaging
- Import the data:
heroku run rake db:import_partners -rstaging
- Import seeded data:
heroku run rake db:seed -rstaging
Example: Add "Procurement" and "Featured_Listing" to Partner Data
-
Create a new Model:
Example: Adding a featured_listing field to Partners:
rails g model featured_listing name:string
Run the migration to reflect the changes in the database:
rails db:migrate
-
Add Relationship associations to Models: *List of Associations
Example:
- Inside app/models/partner.rb add:
belongs_to :featured_listings, optional: true
- Inside app/models/featured_listing.rb add:
within class definition
has_many :partners
- Inside app/models/partner.rb add:
-
When your New Field has a many->many relationship with Partner: Choose a.
When your New Field has a one->many relationship with Partner: Choose b.
a. Create a Join table using a Migration:
rails g migration CreateProcurementsPartnersJoinTable
-
Run the migration to reflect the changes in the database:
rails db:migrate
-
Go to "routes.rb" and add:
resources :procurements
within thenamespace: admin
-
Go to "lib/tasks/db.rake" and add:
procurement_names = val['Procurement'].to_s().split(', ') procurement_names.each do |procurement_name| unless procurement_name.blank? procurement = Procurement.find_or_create_by(name: procurement_name) procurement.partners << partner end end end
b. Generate a migration to create featured_listing_id into the partner table:
rails generate migration AddFeaturedListingToPartners
-
Add the following line to the new migration file inside "db/migrate/(ordered by date)":
class AddFeaturedListingToPartners < ActiveRecord::Migration[5.2] def change add_reference :partners, :featured_listing end end
-
Run the migration to reflect the changes in the database:
rails db:migrate
-
Open your schema file "db/schema.rb" Now you can see "featured_listing_id" column in "partners" table
-
Generate one more migration for creating the foreign key:
-
Add "featured_listing_id" as a foreign key into the partner:
Run:rails g migration AddForeignKeyToPartner
-
Add the following line to the new migration file inside "db/migrate/(ordered by date)":
class AddForeignKeyToTask < ActiveRecord::Migration[5.2] def change add_foreign_key :partners, :featured_listings``` end end
-
Go to "lib/tasks/db.rake" and add:
featured_listing_name = val['Featured Listing'] unless featured_listing_name.blank? featured_listing = FeaturedListing.find_or_create_by(name: featured_listing_name) featured_listing.partners << partner end end
-
-
Run the migration to reflect the changes in the database:
rails db:migrate
*Refer to the "To Import New Data" section in the README
-
-
Go to the "app/views/admin/partners/show.html.haml" file and add code wherever you want to see new field appear:
-
Refer to the "To add new Data Fields" Section inside this README to learn how to tear down the database and import new
Run: rails g controller <path/<controller_name> <action>
Example: rails g controller admin/featured_listings create
Run: rails g model <model_name> <attributes></attributes>name:string
To undo creating a model:
rails destroy model featured_listing
In user.rb, the user model, add a role to the roles enum
In application_controller.rb, in controllers, you may need to edit permissions given to certain roles. For example, for admin we have the function
def restrict_unless_admin
redirect_to(root_url, alert: 'Access denied.') unless current_user.admin? || current_user.superadmin?
end
Were you can adjust any admin restricted view through the controllers by role. You can add roles with ||
or remove roles from functions like this. If you write another function to restrict by role, you can add that function to the top of the controller you would like to affect. The way the above code is implemented in a controller is with the line:
before_action :restrict_unless_admin
Many role restrictions are defined within views (you may want to change this) Check the following files for editing view restrictions: *application.html.haml in views *any of the show.html.haml files for each model view *any of the index.html.haml files for each model view
Note: See .env.example for a complete list of expected environment variables that need set in both staging & production environments. © 2020 Yong Joseph Bakos and Brayden Brown. All rights reserved.