Skip to content

Commit

Permalink
Refactor table name calls
Browse files Browse the repository at this point in the history
THis moves the camelization of singular_table_name and plural_table_name calls to its own method.
  • Loading branch information
jho406 committed Nov 27, 2024
1 parent c87f93a commit 75b349e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
// import { useSelector } from 'react-redux'

export default function <%= plural_table_name.camelize %>Edit ({
export default function <%= js_plural_table_name(:upper) %>Edit ({
// visit,
// remote,
form,
errors,
<%= singular_table_name.camelize(:lower) %>Path,
<%= plural_table_name.camelize(:lower) %>Path,
<%= js_singular_table_name %>Path,
<%= js_plural_table_name %>Path,
}) {
const messagesEl = errors && (
<div id="error_explanation">
Expand All @@ -28,8 +28,8 @@ export default function <%= plural_table_name.camelize %>Edit ({
<button {...form.inputs.submit} type="submit"> {...form.inputs.submit.text} </button>
</form>

<a href={<%= singular_table_name.camelize(:lower) %>Path} data-sg-visit>Show</a>
<a href={<%= plural_table_name.camelize(:lower) %>Path} data-sg-visit>Back</a>
<a href={<%= js_singular_table_name %>Path} data-sg-visit>Show</a>
<a href={<%= js_plural_table_name %>Path} data-sg-visit>Back</a>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import React from 'react'
import { useSelector } from 'react-redux'

export default function <%= plural_table_name.camelize %>Index({
export default function <%= js_plural_table_name(:upper) %>Index({
// visit,
// remote,
new<%= singular_table_name.camelize %>Path,
<%= plural_table_name.camelize(:lower) %> = [],
new<%= js_singular_table_name(:upper) %>Path,
<%= js_plural_table_name %> = [],
}) {
const flash = useSelector((state) => state.flash)

const <%= singular_table_name.camelize(:lower) %>Items = <%= plural_table_name.camelize(:lower) %>.map((<%= singular_table_name.camelize(:lower) %>, key) => {
const deleteForm = <%=singular_table_name.camelize(:lower)%>.deleteForm;
const <%= js_singular_table_name %>Items = <%= js_plural_table_name %>.map((<%= js_singular_table_name %>, key) => {
const deleteForm = <%=js_singular_table_name%>.deleteForm;

return (
<tr key={<%= singular_table_name.camelize(:lower) %>.id}>
<tr key={<%= js_singular_table_name %>.id}>
<%- attributes_list.select{|attr| attr != :id }.each do |attr| -%>
<td>{<%=singular_table_name.camelize(:lower)%>.<%=attr.camelize(:lower)%>}</td>
<td>{<%=js_singular_table_name%>.<%=attr.camelize(:lower)%>}</td>
<%- end -%>
<td><a href={ <%=singular_table_name%>.<%=singular_table_name.camelize(:lower)%>Path } data-sg-visit>Show</a></td>
<td><a href={ <%=singular_table_name%>.edit<%=singular_table_name.camelize%>Path } data-sg-visit>Edit</a></td>
<td><a href={ <%=js_singular_table_name%>.<%=js_singular_table_name%>Path } data-sg-visit>Show</a></td>
<td><a href={ <%=js_singular_table_name%>.edit<%=js_singular_table_name(:upper)%>Path } data-sg-visit>Edit</a></td>
<td>
<form {...deleteForm.props} data-sg-visit>
{Object.values(deleteForm.extras).map((hiddenProps) => (<input {...hiddenProps} key={hiddenProps.id} type="hidden"/>))}
Expand All @@ -33,7 +33,7 @@ export default function <%= plural_table_name.camelize %>Index({
<div>
<p id="notice">{flash && flash.notice}</p>

<h1><%= plural_table_name.capitalize %></h1>
<h1><%= js_plural_table_name(:upper) %></h1>

<table>
<thead>
Expand All @@ -50,7 +50,7 @@ export default function <%= plural_table_name.camelize %>Index({
</tbody>
</table>
<br />
<a href={new<%= singular_table_name.camelize %>Path} data-sg-visit>New <%= singular_table_name.capitalize %></a>
<a href={new<%= js_singular_table_name(:upper) %>Path} data-sg-visit>New <%= js_singular_table_name(:upper) %></a>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react'
// import { useSelector } from 'react-redux'

export default function <%= plural_table_name.camelize %>New({
export default function <%= js_plural_table_name(:upper) %>New({
// visit,
// remote
form,
errors,
<%= plural_table_name.camelize(:lower) %>Path,
<%= js_plural_table_name %>Path,
}) {
const messagesEl = errors && (
<div id="error_explanation">
Expand All @@ -27,7 +27,7 @@ export default function <%= plural_table_name.camelize %>New({
<button {...form.inputs.submit} type="submit"> {...form.inputs.submit.text} </button>
</form>

<a href={<%= plural_table_name.camelize(:lower) %>Path} data-sg-visit>Back</a>
<a href={<%= js_plural_table_name %>Path} data-sg-visit>Back</a>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react'
import { useSelector } from 'react-redux'

export default function <%= plural_table_name.camelize %>Show({
export default function <%= js_plural_table_name(:upper) %>Show({
// visit,
// remote,
<%- attributes_list_with_timestamps.select{|attr| attr != :id }.each do |attr| -%>
<%=attr.camelize(:lower)%>,
<%- end -%>
edit<%= singular_table_name.camelize %>Path,
<%= plural_table_name.camelize(:lower) %>Path
edit<%= js_singular_table_name(:upper) %>Path,
<%= js_plural_table_name %>Path
}) {
const flash = useSelector((state) => state.flash)

Expand All @@ -21,8 +21,8 @@ export default function <%= plural_table_name.camelize %>Show({
{<%=attr.camelize(:lower)%>}
</p>
<%- end -%>
<a href={ edit<%= singular_table_name.camelize %>Path } data-sg-visit>Edit</a>
<a href={ <%= plural_table_name.camelize(:lower) %>Path } data-sg-visit>Back</a>
<a href={ edit<%= js_singular_table_name(:upper) %>Path } data-sg-visit>Edit</a>
<a href={ <%= js_plural_table_name %>Path } data-sg-visit>Back</a>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ end
json.form(partial: 'form') do
end

json.<%= singular_table_name.camelize(:lower)%>Path <%= singular_table_name%>_path(@<%=singular_table_name%>)
json.<%= plural_table_name.camelize(:lower) %>Path <%= plural_table_name %>_path
json.<%= js_singular_table_name%>Path <%= singular_table_name%>_path(@<%=singular_table_name%>)
json.<%= js_plural_table_name %>Path <%= plural_table_name %>_path
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
json.<%= plural_table_name.camelize(:lower) %> do
json.<%= js_plural_table_name %> do
json.array! @<%= plural_table_name %> do |<%= singular_table_name %>|
<%- attributes_list_with_timestamps.each do |attr| -%>
json.<%=attr%> <%= singular_table_name.camelize(:lower) %>.<%=attr%>
json.<%=attr%> <%= js_singular_table_name %>.<%=attr%>
<%- end -%>
json.edit<%=singular_table_name.camelize%>Path edit_<%=singular_table_name%>_path(<%=singular_table_name%>)
json.<%=singular_table_name.camelize(:lower)%>Path <%=singular_table_name%>_path(<%=singular_table_name%>)
json.edit<%=js_singular_table_name(:upper)%>Path edit_<%=singular_table_name%>_path(<%=singular_table_name%>)
json.<%=js_singular_table_name%>Path <%=singular_table_name%>_path(<%=singular_table_name%>)
json.deleteForm do
form_props(model: <%=singular_table_name%>, method: :delete)
end
end
end

json.new<%= singular_table_name.camelize %>Path new_<%= singular_table_name %>_path
json.new<%= js_singular_table_name(:upper) %>Path new_<%= singular_table_name %>_path
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ end
json.form(partial: 'form') do
end

json.<%= plural_table_name.camelize(:lower) %>Path <%= plural_table_name %>_path
json.<%= js_plural_table_name %>Path <%= plural_table_name %>_path


Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
json.<%=attr.to_s.camelize(:lower)%> @<%= singular_table_name %>.<%=attr%>
<%- end -%>

json.<%= plural_table_name.camelize(:lower) %>Path <%= plural_table_name %>_path
json.edit<%= singular_table_name.camelize %>Path edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
json.<%= js_plural_table_name %>Path <%= plural_table_name %>_path
json.edit<%= js_singular_table_name(:upper) %>Path edit_<%= singular_table_name %>_path(@<%= singular_table_name %>)
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def append_mapping

protected

def js_singular_table_name(casing = :lower)
singular_table_name.camelize(casing)
end

def js_plural_table_name(casing = :lower)
plural_table_name.camelize(casing)
end

def available_views
%w(index edit show new)
end
Expand Down

0 comments on commit 75b349e

Please sign in to comment.