Skip to content

Instantly share code, notes, and snippets.

@norbajunior
Forked from danielvlopes/roteiro.md
Created December 7, 2011 14:27
Show Gist options
  • Save norbajunior/1442999 to your computer and use it in GitHub Desktop.
Save norbajunior/1442999 to your computer and use it in GitHub Desktop.
Roteiro Aula 6 - Una Web 2

Apagar action show de skills_controller.rb

Em application.html.erb alterar o menu para:

-    <li><%= link_to "Skills", skills_path %></li>

Remover indicações de profile nas views de Skills:

-    <th>Profile</th>
-    <td><%= skill.profile %></td>
-    <td><%= link_to 'Show', skill %></td>

Remover arquivo show.html.erb de skills.

Ajustar rotas:

Resume::Application.routes.draw do
  scope "/admin" do
    resources :skills, :except => [:show]
    resources :employments
  end

  root :to => "resumes#show"
  get "resumes/show"
end

Adicionar profile to employment (sempre rodar "rake db:migrate" depois de criar migrations)

rails generate migration add_profile_id_to_employments profile_id:integer

Ajustar model de employment e profile

Class Employment < ActiveRecord::Base

  belongs_to :profile

  ...

Class Profile < ActiveRecord::Base

  has_many :employments

  ...

Em employments_controller carregar os profiels:

class EmploymentsController < ApplicationController

  before_filter :load_profiles, :only => [:new, :edit, :create, :update]

  ...

private

  def load_profiles
    @profiles = Profile.all
  end

end

Em _form.html.erb de employments adicionar:

<div class="field">
  <%= f.label :profile_id, "Profile" %><br />
  <%= f.collection_select :profile_id, @profiles, :id, :full_name %>
</div>

Também adicionar description em employment e tmb alguns outros campos em profile:

rails generate migration add_description_to_employments description:text
rails generate migration add_phone_and_site_to_profiles phone:string site:string

Em seguida ajustar a tela:

<div class="field">
  <%= f.label :description %><br />
  <%= f.text_area :description, :style => "width: 80%; height: 100px;" %>
</div>

Ajustar rotas de resumes deixando o arquivo todo como abaixo:

Resume::Application.routes.draw do

  scope "/admin" do
    resources :skills, :except => [:show]
    resources :employments
    resources :profiles
  end

  resources :resumes

  root :to => "resumes#show"
end

Ajustar ResumesController para ser dinâmico:

class ResumesController < ApplicationController

  layout "curriculum"

  def show
    profile = Profile.find(params[:id])
    @resume = ResumePresenter.new(profile)
  end

end

Criar um presenter Resume:

class ResumePresenter

  attr_accessor :profile

  delegate :full_name, :job_title, :email, :site, :phone,
           :to => :profile

  def initialize(profile)
    @profile = profile
  end

  def employments
    @profile.employments
  end

  def skills
    @profile.skills
  end

end

Ajustar show de ResumesController como abaixo:

<div id="wrapper">
  <div id="bio">
    <h1><%= @resume.full_name %></h1>
    <h2><%= @resume.job_title %></h2>

    <div id="contacts">
      <p><span>fone</span> <%= @resume.phone %> </p>
      <p><span>email</span> <%= @resume.email %> </p>
      <p><span>site</span>  <%= @resume.site %> </p>
    </div>
  </div><!-- bio -->

  <div id="column1">
    <div id="employments">

      <% @resume.employments.each do |employment| %>

      <div class="employment">
        <div class="dateInterval"><%= date_interval employment %></div>
        <div class="description">
          <h3><%= employment.contractor %></h3>
          <h4><%= employment.job_title %></h4>
          <p><%= employment.description %></p>
        </div>
      </div>

      <% end %>

    </div>
  </div> <!-- column1 -->

  <div id="column2">
    <div id="skills">
      <h3>Conhecimentos</h3>
      <ul>
        <% @resume.skills.each do |skill| %>
        <li><%= skill.name %> <span><%= skill.experience %></span></li>
        <% end %>
      </ul>
    </div>
  </div>

</div><!-- wrapper -->

Criar método date_interval em ResumesHelper:

#encoding: utf-8

module ResumesHelper

  def date_interval(employment)
    "#{l employment.started_on} até #{l employment.ended_on }"
  end

end

Por último criar as telas para administrar profiles:

rails generate scaffold Profile first_name:string last_name:string email:string job_title:string phone:string site:string --skip-migration

Seguido de ajustes no form de profiles:

<%= form_for(@profile, :html => { :class => "form-stacked" }) do |f| %>

  <%= error_messages_for @profile %>

  ...

  <div class="actions">
    <%= f.submit :class => "btn" %>
  </div>

<% end %>

Para encerrar basta criar um arquivo pt-BR.yml dentro da pasta config/locales com o conteúdo deste link:

https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/pt-BR.yml

Em em config/application.rb alterar a linha:

# config.i18n.default_locale = :de

Para

config.i18n.default_locale = "pt-BR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment