Просмотр исходного кода

Add some basic scaffold overrides that use Bootstrap

Chris Oliver 6 лет назад
Родитель
Сommit
63d6cd3bd0

+ 46 - 0
lib/templates/erb/scaffold/_form.html.erb

@@ -0,0 +1,46 @@
+<%%= form_with(model: <%= model_resource_name %>, local: true) do |form| %>
+  <%% if <%= singular_table_name %>.errors.any? %>
+    <div id="error_explanation">
+      <h2><%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
+
+      <ul>
+      <%% <%= singular_table_name %>.errors.full_messages.each do |message| %>
+        <li><%%= message %></li>
+      <%% end %>
+      </ul>
+    </div>
+  <%% end %>
+
+<% attributes.each do |attribute| -%>
+  <div class="form-group">
+<% if attribute.password_digest? -%>
+    <%%= form.label :password %>
+    <%%= form.password_field :password, class: 'form-control' %>
+  </div>
+
+  <div class="form-group">
+    <%%= form.label :password_confirmation %>
+    <%%= form.password_field :password_confirmation, class: 'form-control' %>
+<% else -%>
+    <%%= form.label :<%= attribute.column_name %> %>
+    <%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: 'form-control' %>
+<% end -%>
+  </div>
+
+<% end -%>
+  <div class="form-group">
+    <%% if <%= model_resource_name %>.persisted? %>
+      <div class="float-right">
+        <%%= link_to 'Destroy', <%= model_resource_name %>, method: :delete, class: "text-danger", data: { confirm: 'Are you sure?' } %>
+      </div>
+    <%% end %>
+
+    <%%= form.submit class: 'btn btn-primary' %>
+
+    <%% if <%= model_resource_name %>.persisted? %>
+      <%%= link_to "Cancel", <%= model_resource_name %>, class: "btn btn-link" %>
+    <%% else %>
+      <%%= link_to "Cancel", <%= index_helper %>_path, class: "btn btn-link" %>
+    <%% end %>
+  </div>
+<%% end %>

+ 3 - 0
lib/templates/erb/scaffold/edit.html.erb

@@ -0,0 +1,3 @@
+<h1>Edit <%= singular_table_name.capitalize %></h1>
+
+<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>

+ 51 - 0
lib/templates/erb/scaffold/index.html.erb

@@ -0,0 +1,51 @@
+<% name_attribute = attributes.find{ |a| a.name == "name" } %>
+<% has_name = !!name_attribute %>
+
+<div class="row">
+  <div class="col-sm-6">
+    <h1><%= plural_table_name.capitalize %></h1>
+  </div>
+
+  <div class="col-sm-6 text-right">
+  <%%= link_to new_<%= singular_table_name %>_path, class: 'btn btn-primary' do %>
+    Add New <%= human_name %>
+  <%% end %>
+  </div>
+</div>
+
+<div class="table-responsive">
+  <table class="table table-striped table-bordered table-hover">
+    <thead>
+      <tr>
+    <% if has_name %>
+        <th>Name</th>
+    <% end %>
+
+    <% attributes.without(name_attribute).each do |attribute| -%>
+        <th><%= attribute.human_name %></th>
+    <% end -%>
+        <% unless has_name %>
+          <th></th>
+        <% end %>
+      </tr>
+    </thead>
+
+    <tbody>
+      <%% @<%= plural_table_name%>.each do |<%= singular_table_name %>| %>
+        <%%= content_tag :tr, id: dom_id(<%= singular_table_name %>), class: dom_class(<%= singular_table_name %>) do %>
+          <% if has_name %>
+            <td><%%= link_to <%= singular_table_name %>.name, <%= singular_table_name %> %></td>
+          <% end %>
+
+          <% attributes.without(name_attribute).each do |attribute| -%>
+            <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
+          <% end -%>
+
+          <% unless has_name %>
+            <td><%%= link_to 'Show', <%= singular_table_name %> %></td>
+          <% end %>
+        <%% end %>
+      <%% end %>
+    </tbody>
+  </table>
+</div>

+ 3 - 0
lib/templates/erb/scaffold/new.html.erb

@@ -0,0 +1,3 @@
+<h1>New <%= singular_table_name %></h1>
+
+<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>

+ 19 - 0
lib/templates/erb/scaffold/show.html.erb

@@ -0,0 +1,19 @@
+<div class="page-header">
+  <%%= link_to <%= index_helper %>_path, class: 'btn btn-default' do %>
+    <span class="glyphicon glyphicon-list-alt"></span>
+    All <%= plural_table_name.capitalize %>
+  <%% end %>
+  <%%= link_to edit_<%= singular_table_name %>_path(@<%= singular_table_name %>), class: 'btn btn-primary' do %>
+    <span class="glyphicon glyphicon-pencil"></span>
+    Edit
+  <%% end %>
+  <h1>Show <%= singular_table_name %></h1>
+</div>
+
+<dl class="dl-horizontal">
+  <%- attributes.each do |attribute| -%>
+  <dt><%= attribute.human_name %>:</dt>
+  <dd><%%= @<%= singular_table_name %>.<%= attribute.name %> %></dd>
+
+  <%- end -%>
+</dl>