Skip to content

Commit

Permalink
Merge branch 'main' of github.com:renuo/rails_db_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ignaciosy committed Oct 23, 2024
2 parents c3c50f1 + 50e9932 commit abd1806
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: ci

on: push

jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Install Dependencies
uses: ruby/setup-ruby@v1.197.0
with:
bundler-cache: true

- name: Run linters
run: bundle exec rubocop -c .rubocop.yml

- name: Run tests
run: RAILS_ENV=test bundle exec rspec
2 changes: 1 addition & 1 deletion app/controllers/hotsheet/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def model_params
end

def model_class
params[:model].constantize
params[:model]&.constantize
end
end
end
19 changes: 9 additions & 10 deletions config/initializers/hotsheet/editable_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

Rails.application.config.after_initialize do # rubocop:disable Metrics/BlockLength
Hotsheet.configuration.models.each_key do |model| # rubocop:disable Metrics/BlockLength
model = model.to_s.constantize

model.class_eval do
model.constantize.class_eval do
class << self
def editable_attributes
@editable_attributes ||= fetch_editable_attributes
Expand All @@ -13,14 +11,15 @@ def editable_attributes
private

def fetch_editable_attributes
config = Hotsheet.configuration.models[name.to_sym]
config = Hotsheet.configuration.models[name]
excluded_attributes = config.excluded_attributes

if config.key?(:included_attributes)
raise "Can only specify either included or excluded attributes" if config.key?(:excluded_attributes)
if config.included_attributes.present?
raise "Can only specify either included or excluded attributes" if excluded_attributes.present?

attrs_to_s(config[:included_attributes])
elsif config.key?(:excluded_attributes)
column_names - attrs_to_s(config[:excluded_attributes])
attrs_to_s(config.included_attributes)
elsif excluded_attributes.present?
column_names - attrs_to_s(excluded_attributes)
else
column_names
end
Expand All @@ -34,6 +33,6 @@ def attrs_to_s(attrs)
end
end

model.editable_attributes
model.constantize.editable_attributes
end
end
2 changes: 1 addition & 1 deletion lib/hotsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def configure
end

def models
configuration.models.each_key.map { |model| model.to_s.constantize }
@models ||= configuration.models.each_key.map(&:constantize)
end
end
end
10 changes: 10 additions & 0 deletions lib/hotsheet/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@ class Configuration
include ActiveSupport::Configurable

config_accessor(:models) { {} }

def model(name, &)
model_config = ModelConfig.new
yield model_config
models[name.to_s] = model_config
end

class ModelConfig
attr_accessor :included_attributes, :excluded_attributes
end
end
end

0 comments on commit abd1806

Please sign in to comment.