weteamsteve / squadsnap2

The #2 sports team management app that makes communication and organization a breeze. Clubs and leagues love us too. Try it now.
1 stars 0 forks source link

Implement squads and members functionality #2

Open weteamsteve opened 5 years ago

weteamsteve commented 5 years ago

Implement squads model and associations from squadsnap

Reference squadsnap project, as well as the squadsnap/wiki/Models page.

Reference How to Set Up a Ruby on Rails Project with a React Frontend

weteamsteve commented 5 years ago

create members migration rails g model Member

      t.references :user, foreign_key: true
      t.string :position
      t.string :number
      t.string :membership # ie owner, manager, member, request, ghost

create squads migration rails g model Squad

      t.string :name
      t.string :sport
      t.references :owner, index: true, foreign_key: { to_table: :users }

add squad to members migration rails g migration AddSquadToMember squad:references

    add_reference :members, :squad, null: false, index: true, foreign_key: true
weteamsteve commented 5 years ago

rails generate controller api/v1/Squads index create show destroy -j=false -y=false --skip-template-engine --no-helper

weteamsteve commented 5 years ago
weteamsteve commented 5 years ago

How are we going to access member data from the squads area on the frontend. Check out how to include rails associated model data in react components

However, unlike in a Rails view, if we call message.user.name inside our React component without having specifically included that data in the prop sent to the component, it will throw an error.

While a Rails template is actually able to call the model on-the-fly and get data it doesn't have, we don't have that luxury with React.

We need to explicitly compose the JSON with the associated User model data because a React component can only access the JSON data we provide it.

There are many ways to include the associated model data, including manually composing the JSON in the controller, defining a custom as_json method on the model or using ActiveModelSerializers.

One of the cleanest and most flexible ways is to use jbuilder, which gives you a simple DSL for declaring JSON structures. The jbuilder gem comes included with Rails.

In the example above, we can include our user data in the message JSON by defining it in a _message.json.jbuilder file like this:

json.(message, :body, :id)
json.user do
  json.extract! message.user, :id, :name, :image
end

The jbuilder DSL is quite powerful and allows you to do all sorts of custom structuring of the data.

For example, let's say we want to send all the messages in chronological order and user data for a particular chatroom to a Chatroom component. We can define the chatroom json like this:

json.(chatroom, :name, :id)
json.messages(chatroom.messages
                .sort_by{|m| m[:created_at]}) do |message|
  json.extract! message, :id, :body
  json.user do
    json.extract! message.user, :id, :name, :image
  end
end

Also check out rails building json api responses with jbuilder