Skip to content

Instantly share code, notes, and snippets.

@oliveira-andre
Last active December 2, 2019 13:36
Show Gist options
  • Save oliveira-andre/ceeeac9990f325610e4ea590ba25fdca to your computer and use it in GitHub Desktop.
Save oliveira-andre/ceeeac9990f325610e4ea590ba25fdca to your computer and use it in GitHub Desktop.
rails 6 annotations

coffee and jquery

  • Coffee and another gems like jquery is installed on yarn and imported on app/javascripts/application.js

    rails webpacker:install:coffee

    References

    https://bloggie.io/@kinopyo/rails-enable-coffeescript-with-webpacker
    

Importing all files from folder

  • Now when you want to create a separated folder to your javascripts you need to import like bellow, bellow my folder is on app/javascripts/controllers:

    $ = require('jquery')
    require('controllers')
  • Inside this folder you need the archive index.js what will contains the following code to import all files

    const channels = require.context('.', true, /\.coffee$/)
    channels.keys().forEach(channels)

action mailbox

  • Another form to send emails now, with a simple and completed type to configure the api to send emails, callbacks and a lot of more

    rails action_mailbox:install
    rails db:migrate
    rails credentials:edit

    To use mailgun use configurations bellow:

    action_mailbox:
      mailgun_api_key: ...

    production.rb

    config.action_mailbox.ingress = :mailgun

    application_mailbox.rb

    class ApplicationMailbox < ActionMailbox::Base
      routing /^save@/i     => :forwards
      routing /@replies\./i => :replies
    end
    rails generate mailbox forwards

    forwards_mailbox.rb

    class ForwardsMailbox < ApplicationMailbox
      # Callbacks specify prerequisites to processing
      before_processing :require_forward
     
      def process
        if forwarder.buckets.one?
          record_forward
        else
          stage_forward_and_request_more_details
        end
      end
     
      private
        def require_forward
          unless message.forward?
            # Use Action Mailers to bounce incoming emails back to sender – this halts processing
            bounce_with Forwards::BounceMailer.missing_forward(
              inbound_email, forwarder: forwarder
            )
          end
        end
     
        def forwarder
          @forwarder ||= Person.where(email_address: mail.from)
        end
     
        def record_forward
          forwarder.buckets.first.record \
            Forward.new forwarder: forwarder, subject: message.subject, content: mail.content
        end
     
        def stage_forward_and_request_more_details
          Forwards::RoutingMailer.choose_project(mail).deliver_now
        end
    end

    References:

    https://guides.rubyonrails.org/action_mailbox_basics.html
    

action text

  • well i think that i will use it so much now the rails action text to get a rich text to get text bold or underlined etc...
rails action_text:install

app/models/message.rb

class Message < ApplicationRecord
  has_rich_text :content
end

app/views/messages/_form.html.erb

<%= form_with model: message do |form| %>
  <div class="field">
    <%= form.label :content %>
    <%= form.rich_text_area :content %>
  </div>
<% end %>

show this content whatever you want like: app/views/messages/show.html.erb

<%= @message.content %>

app/controllers/messages_controller.rb

class MessagesController < ApplicationController
  def create
    message = Message.create! params.require(:message).permit(:title, :content)
    redirect_to message
  end
end

New commands

User.pick(:name) || User.find(params[:id]).pick(:name) # it is like pluck
rails db:prepare
insert_all || insert_all! || upsert_all # active record changes
:if_not_exists option to create_table...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment