Skip to content

Commit

Permalink
Add more models
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/views/layouts/hotsheet/_sidebar.html.erb
  • Loading branch information
hunchr committed Nov 5, 2024
1 parent e434b6c commit 848898e
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/controllers/hotsheet/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def broadcast_params
end

def model_params
params.require(model.name.underscore.to_sym).permit(*model.editable_attributes)
params.require(model.name.underscore).permit(*model.editable_attributes)
end

def model
Expand Down
23 changes: 23 additions & 0 deletions app/views/hotsheet/pages/_editable_attribute.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%# locals: (attribute:, model:, record:) %>

<%= turbo_frame_tag "#{dom_id record}-#{attribute}" do %>
<div data-controller="editable-attribute"
data-editable-attribute-broadcast-url-value="<%= broadcast_edit_intent_path %>"
data-editable-attribute-resource-name-value="<%= model.table_name %>"
data-editable-attribute-resource-id-value="<%= record.id %>">
<div class="readonly-attribute"
data-editable-attribute-target="readonlyAttribute"
data-action="click->editable-attribute#displayInputField">
<%= record.public_send attribute %>
</div>

<div data-editable-attribute-target="attributeForm" style="display:none">
<%= form_for record, url: "#{root_path}#{model.table_name}/#{record.id}" do |f| %>
<%= f.text_field attribute, class: "editable-input", data: {
editable_attribute_target: "attributeFormInput",
action: "keydown.enter->editable-attribute#submitForm blur->editable-attribute#submitForm"
} %>
<% end %>
</div>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/hotsheet/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<tr>
<% @model.editable_attributes.each do |attribute| %>
<td>
<%= render "hotsheet/shared/inline_editable_value", object: record, attribute: attribute %>
<%= render "editable_attribute", model: @model, record: record, attribute: attribute %>
</td>
<% end %>
</tr>
Expand Down
21 changes: 0 additions & 21 deletions app/views/hotsheet/shared/_inline_editable_value.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/layouts/hotsheet/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<nav>
<ul>
<% Hotsheet.models.each do |model| %>
<%= link_to model.model_name.human, polymorphic_path(model.table_name) %>
<%= link_to model.model_name.human, "#{root_path}#{model.table_name}" %>
<% end %>
</ul>
</nav>
Expand Down
7 changes: 6 additions & 1 deletion config/initializers/hotsheet/editable_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

Rails.application.config.after_initialize do # rubocop:disable Metrics/BlockLength
# Only run this initializer when running the Rails server
next unless defined? Rails::Server

Hotsheet.configuration.models.each_key do |model| # rubocop:disable Metrics/BlockLength
model.constantize.class_eval do
class << self
Expand All @@ -27,7 +30,9 @@ def fetch_editable_attributes

def attrs_to_s(attrs)
attrs.map do |attr|
column_names.include?(attr.to_s) ? attr.to_s : raise("Attribute '#{attr}' doesn't exist on model '#{name}'")
raise "Attribute '#{attr}' doesn't exist on model '#{name}'" if column_names.exclude? attr.to_s

attr.to_s
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
post :broadcast_edit_intent, to: "pages#broadcast_edit_intent"

Hotsheet.models.each do |model|
resources model.name.downcase.pluralize.to_sym, controller: "pages", only: %i[index update], model: model.name
resources model.table_name, controller: :pages, only: %i[index update], model: model.name
end
end
5 changes: 5 additions & 0 deletions spec/dummy/app/models/table_name_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class TableNameTest < ApplicationRecord
self.table_name = "different_db_name"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class VeryLongModelNameForOverflowTest < ApplicationRecord
end
8 changes: 8 additions & 0 deletions spec/dummy/config/initializers/hotsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@
config.model :Post do |model|
model.excluded_attributes = %i[id author_id created_at updated_at]
end

config.model :TableNameTest do |model|
model.included_attributes = []
end

config.model :VeryLongModelNameForOverflowTest do
nil
end
end
24 changes: 24 additions & 0 deletions spec/dummy/db/migrate/20241104095444_create_more_models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class CreateMoreModels < ActiveRecord::Migration[8.1]
def change
overflow_test
different_db_name
end

private

def overflow_test
create_table :very_long_model_name_for_overflow_tests do |t|
t.string :even_longer_column_name_for_overflow_test
t.timestamps
end
end

def different_db_name
create_table :different_db_name do |t|
t.string :title
t.timestamps
end
end
end
14 changes: 13 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.1].define(version: 20_241_022_111_111) do
ActiveRecord::Schema[8.1].define(version: 20_241_104_095_444) do
create_table "authors", force: :cascade do |t|
t.string "name"
t.date "birthdate"
Expand All @@ -21,6 +21,12 @@
t.datetime "updated_at", null: false
end

create_table "different_db_name", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "posts", force: :cascade do |t|
t.string "title"
t.string "body"
Expand All @@ -30,5 +36,11 @@
t.index ["author_id"], name: "index_posts_on_author_id"
end

create_table "very_long_model_name_for_overflow_tests", force: :cascade do |t|
t.string "even_longer_column_name_for_overflow_test"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_foreign_key "posts", "authors"
end
13 changes: 9 additions & 4 deletions spec/dummy/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
require "faker"

10.times do
Author.create!(name: Faker::Name.name,
Author.create! name: Faker::Name.name,
birthdate: Faker::Date.birthday(min_age: 18, max_age: 65),
gender: Author.genders.values.sample)
gender: Author.genders.values.sample
end

10.times do
Post.create!(title: Faker::Lorem.word,
Post.create! title: Faker::Lorem.word,
body: Faker::Lorem.paragraph,
author_id: Author.pluck(:id).sample)
author_id: Author.pluck(:id).sample
end

100.times do
VeryLongModelNameForOverflowTest
.create! even_longer_column_name_for_overflow_test: Faker::Lorem.paragraph
end

0 comments on commit 848898e

Please sign in to comment.