Skip to content

Commit

Permalink
Fix bug if attrs are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-isler committed Nov 12, 2024
1 parent 893616d commit 06a01cf
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 53 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).permit(*model.editable_attributes)
params.require(model.name.underscore).permit(*Hotsheet.editable_attributes_for(model: model))
end

def model
Expand Down
4 changes: 2 additions & 2 deletions app/views/hotsheet/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<table>
<thead>
<tr>
<% @model.editable_attributes.each do |attribute| %>
<% Hotsheet.editable_attributes_for(model: @model).each do |attribute| %>
<th><%= attribute %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @records.each do |record| %>
<tr>
<% @model.editable_attributes.each do |attribute| %>
<% Hotsheet.editable_attributes_for(model: @model).each do |attribute| %>
<td>
<%= render "editable_attribute", model: @model, record: record, attribute: attribute %>
</td>
Expand Down
43 changes: 0 additions & 43 deletions config/initializers/hotsheet/editable_attributes.rb

This file was deleted.

8 changes: 4 additions & 4 deletions lib/hotsheet/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ class ModelConfig

def initialize(model_name)
@model_name = model_name
@included_attributes = []
@excluded_attributes = []
end

def validate!
return if included_attributes.nil? && excluded_attributes.nil?

ensure_only_one_attribute_set
validate_attribute_existence
end

private

def ensure_only_one_attribute_set
return unless included_attributes.any? && excluded_attributes.any?
return if included_attributes.blank? || excluded_attributes.blank?

raise "Can only specify either included or excluded attributes for '#{model_name}'"
end
Expand All @@ -44,7 +44,7 @@ def validate_attribute_existence
model_class = model_name.to_s.constantize
all_attributes = model_class.column_names

(included_attributes + excluded_attributes).each do |attr|
[included_attributes, excluded_attributes].flatten.compact.each do |attr|
unless all_attributes.include?(attr.to_s)
raise "Attribute '#{attr}' doesn't exist on model '#{model_name}'"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/hotsheet/editable_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def editable_attributes_for(model:)
def fetch_editable_attributes(model)
model_config = Hotsheet.configuration.models[model.to_s]

if model_config&.included_attributes.present?
if model_config&.included_attributes
model_config.included_attributes.map(&:to_s)
elsif model_config&.excluded_attributes.present?
elsif model_config&.excluded_attributes
model.column_names - model_config.excluded_attributes.map(&:to_s)
else
model.column_names
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/config/initializers/hotsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

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

config.model :VeryLongModelNameForOverflowTest do
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/editable_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@
end
end

context "when included attributes are empty" do
before do
Hotsheet.configure do |config|
config.model :Author do |model|
model.included_attributes = %i[]
end
end
end

it "returns an empty list" do
expect(editable_attributes).to eq %w[]
end
end

context "when excluded attributes are empty" do
before do
Hotsheet.configure do |config|
config.model :Author do |model|
model.excluded_attributes = %i[]
end
end
end

it "returns all attributes" do
expect(editable_attributes).to eq Author.column_names
end
end

context "when no included or excluded attributes are specified" do
before do
Hotsheet.configure do |config|
Expand Down

0 comments on commit 06a01cf

Please sign in to comment.