Skip to content

Latest commit

 

History

History
307 lines (295 loc) · 8.33 KB

README.md

File metadata and controls

307 lines (295 loc) · 8.33 KB

README


Peer.ai Backend

Table of Contents

Chapter 0 - Usage

  • Install Ruby on Rails >5.1.3
  • Install dependencies
    bundle install
    
  • Run PostgreSQL
  • Create and Migrate the database
    bundle exec rails db:drop db:create db:migrate
    
  • Run Guard
    bundle exec guard
    
  • Run API Server
    bundle exec rails s
    
  • Modify RSpec tests and code implementation

Chapter 1 - Setup Ruby on Rails App

rails new --api peerai-backend --database=postgresql --skip-test

Chapter 2 - Setup RSpec Gem and Guard RSpec Gem

Chapter 3 - Setup Devise Gem

Chapter 4 - Setup Act As Taggable Gem

  • Reference: https://github.com/mbleigh/acts-as-taggable-on
  • Add to Gemfile
    gem 'acts-as-taggable-on', '~> 4.0'`
    
  • Remove the following from the Gemfile to prevent error NameError: uninitialized constant SkillToken::ActsAsTree
    # gem 'spring'
    # gem 'spring-watcher-listen', '~> 2.0.0'
    
  • Install dependency
    bundle install
  • Generate migration files in db/migrate
    rails acts_as_taggable_on_engine:install:migrations
  • Run PostgreSQL server
  • Migrate migrations into PostgreSQL tables rails db:migrate
    
    

Chapter 5 - Setup Act As Tree Gem with Profile SkillToken Model

  • Add Profile model

    bundle exec rails g model Profile user:references
    
  • Add associations to User

    has_one :profile, class_name: 'Profile'
    delegate :skill_tokens, :to => :profile
    
  • Add associations to Profile

    belongs_to :user
    has_many :skill_tokens
    
  • Add Parent model for ActsAsTree

    bundle exec rails g model Parent
    
  • Add HABTM association to Parent model

    has_and_belongs_to_many :skill_tokens
    
  • Add SkillToken model with extra attribute parent for ActsAsTree

    bundle exec rails g model SkillToken name:string amount:decimal weight:decimal profile:references parent:references
    
  • Add ActsAsTree Gem to Gemfile

    gem 'acts_as_tree', '~> 2.7.0'
    
  • Add associations to SkillToken

    belongs_to :profile, class_name: 'Profile'
    
  • Add to SkillToken model

    belongs_to :profile, class_name: 'Profile'
    has_and_belongs_to_many :parents
    validates_presence_of :name
    
    extend ActsAsTree::TreeView
    acts_as_tree order: 'name'
    
  • Migrate database

    bundle exec rails db:drop db:create db:migrate
    
  • Run rails

    bundle exec rails s
    
    • Troubleshooting
      • Problem
        $ rails s
        Could not find bcrypt-3.1.11 in any of the sources
        Run `bundle install` to install missing gems.
        
      • Solution
        • Uncomment in Gemfile gem 'bcrypt', '~> 3.1.11'
        • Run gem pristine bcrypt --version 3.1.11
        • Run bundle exec as prefix before any rails command
  • Rails console

    bundle exec rails c
    
    # create user and profile
    user1 = User.create(email: '[email protected]', password: '12345678', encrypted_password: '12345678')
    user1.profile = Profile.create(user_id: user1.id)
    # create root
    root = SkillToken.create(name: "root", weight: 1, profile_id: user1.profile.id)
    # verify that root's parent is nil
    root.parent
    # show the Tree View
    SkillToken.tree_view(:name)
    # create parent root
    parent1 = Parent.create(id: 1)
    # create child1 with root as parent
    child1 = root.children.create(name: "child1", weight: 1, profile_id: user1.profile.id, parent_id: parent1.id)
    # create parent of sub-child
    parent2 = Parent.create(id: child1.id)
    # create sub-child1 of child1
    subchild1 = child1.children.create(name: "subchild1", weight: 1, profile_id: user1.profile.id, parent_id: child1.id)
    # show parents
    Parent.all
    # create sub-child2 of child1
    subchild2 = child1.children.create(name: "subchild2", weight: 1, profile_id: user1.profile.id, parent_id: child1.id)
    # show the Tree View
    SkillToken.tree_view(:name)
    # |_ root
    #   |_ child1
    #     |_ subchild1
    #     |_ subchild2
    # show subchildren
    root.children.first.children
    
    • Note: Run the following if existing data previously added to the DB tables
      user1 = User.all.first
      user1.profile = Profile.all.first
      root = SkillToken.all.first
      root.children
      root.children.first.children
      
  • Note: