Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radius Custom Templates #16

Merged
merged 4 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/radius-rails.rb
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
require "radius/rails"

module RadiusRails
sam-kim marked this conversation as resolved.
Show resolved Hide resolved
class Railtie < Rails::Railtie
config.app_generators do |g|
g.templates.unshift File::expand_path('../templates', __FILE__)
sam-kim marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
2 changes: 1 addition & 1 deletion lib/radius/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Radius
module Rails
VERSION = "2.0.0"
VERSION = "2.1.0"
end
end
67 changes: 67 additions & 0 deletions lib/templates/rails/scaffold_controller/controller.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

<% if namespaced? -%>
require_dependency "<%= namespaced_path %>/application_controller"

<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
before_action :set_<%= singular_table_name %>, only: %i[show edit update destroy]

def index
@<%= plural_table_name %> = <%= singular_table_name.capitalize %>.accessible_by_user(current_user).all
end

def show
end

def new
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
end

def edit
end

def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>

if @<%= orm_instance.save %>
redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
else
render :new
end
end

def update
if @<%= orm_instance.update("#{singular_table_name}_params") %>
redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
else
render :edit
end
end

def destroy
@<%= orm_instance.destroy %>!
redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %>
end

private

def set_<%= singular_table_name %>
@<%= singular_table_name %> = <%= singular_table_name.capitalize %>.accessible_by_user(current_user).find(params[:id])
end

def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params.fetch(:<%= singular_table_name %>, {})
<%- else -%>
params.require(:<%= singular_table_name %>)
.permit(
<% attributes_names.each do |name| -%>
:<%= name %>,
<% end -%>
)
<%- end -%>
end
end
<% end -%>
44 changes: 44 additions & 0 deletions lib/templates/slim/scaffold/_form.html.slim.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
= form_for @<%= singular_table_name %> do |f|
.row
.col-lg-8
= render "layouts/partials/form_errors", resource: @<%= singular_table_name %>

.row
.col-lg-8
.panel.panel-default
.panel-heading
.panel-title <%= singular_table_name.titleize %> Details
.panel-body
<% attributes.each do |attribute| -%>
.form-group.has-feedback
<% if attribute.password_digest? -%>
= f.label :password
= f.password_field :password, class: "form-control"
.form-group.has-feedback
= f.label :password_confirmation
= f.password_field :password_confirmation, class: "form-control"
<% else -%>
= f.label :<%= attribute.column_name %>
<% case attribute.field_type -%>
<% when :check_box -%>
.checkbox.c-checkbox.mt0
label
= f.check_box :<%= attribute.column_name %>, class: 'form-control'
span.fa.fa-check
sam-kim marked this conversation as resolved.
Show resolved Hide resolved
<% when :datetime_select -%>
.datepicker.input-group.date
= f.text_field :<%= attribute.column_name %>, class: 'form-control'
span.input-group-addon
span.fa.fa-calendar
<% else -%>
= f.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "form-control"
<% end -%>
<% end -%>
<% end -%>

.row
.col-lg-8
.panel.panel-default
.panel-body
= f.submit class: "btn btn-sm pull-right btn-primary"
= link_to 'Cancel', <%= index_helper %>_url, class: "btn btn-sm btn-default"
10 changes: 10 additions & 0 deletions lib/templates/slim/scaffold/edit.html.slim.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.content-heading
| Edit <%= singular_table_name.titleize %>

ol.breadcrumb
li
= link_to "<%= plural_table_name.titleize %>", <%= index_helper %>_url
li
= link_to "Back", @<%= singular_table_name %>

= render partial: 'form'
27 changes: 27 additions & 0 deletions lib/templates/slim/scaffold/index.html.slim.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.content-heading
.btn-toolbar.pull-right
| <%= plural_table_name.titleize %>
| <%= plural_table_name.titleize %>

ol.breadcrumb
li = link_to '<%= plural_table_name.titleize %>', <%= index_helper %>_path

.row
.col-sm-12
.panel.panel-default
.panel-heading
| My <%= plural_table_name.titleize %>

table.table.table-hover.table-generic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to move the .table-generic and any related styling over too.

thead
tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
th <%= attribute.human_name %>
<% end -%>

tbody
- @<%= plural_table_name %>.each do |<%= singular_table_name %>|
tr
<% attributes.reject(&:password_digest?).each do |attribute| -%>
td = <%= singular_table_name %>.<%= attribute.name %>
<% end -%>
8 changes: 8 additions & 0 deletions lib/templates/slim/scaffold/new.html.slim.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.content-heading
| New <%= singular_table_name.titleize %>

ol.breadcrumb
li
= link_to "<%= plural_table_name.titleize %>", <%= index_helper %>_url

= render partial: 'form'
11 changes: 11 additions & 0 deletions lib/templates/slim/scaffold/show.html.slim.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
p#notice = notice

<% attributes.each do |attribute| -%>
p
strong <%= attribute.human_name %>:
= @<%= singular_table_name %>.<%= attribute.name %>
<% end -%>

=> link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
'|
=< link_to 'Back', <%= index_helper %>_path
sam-kim marked this conversation as resolved.
Show resolved Hide resolved