Skip to content

Commit

Permalink
Refactor for Code Climate
Browse files Browse the repository at this point in the history
- merged two methods to reduce duplicate logic
- simplified require for active_storage install
  • Loading branch information
Don Sisco authored and scambra committed Dec 16, 2019
1 parent c4d73c0 commit 91bbec7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
8 changes: 1 addition & 7 deletions lib/active_scaffold/bridges/active_storage.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
class ActiveScaffold::Bridges::ActiveStorage < ActiveScaffold::DataStructures::Bridge
def self.install
if ActiveScaffold::Config::Core.method_defined?(:initialize_with_active_storage)
raise "We've detected that you have active_scaffold_active_storage_bridge installed. This plugin has been moved to core. Please remove active_scaffold_active_storage_bridge to prevent any conflicts"
end
require File.join(File.dirname(__FILE__), 'active_storage/active_storage_bridge')
require File.join(File.dirname(__FILE__), 'active_storage/form_ui')
require File.join(File.dirname(__FILE__), 'active_storage/list_ui')
require File.join(File.dirname(__FILE__), 'active_storage/active_storage_helpers')
Dir[File.join(__dir__, 'active_storage', '*.rb')].each { |file| require file }
ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::ActiveStorage::ActiveStorageBridge
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,17 @@ def initialize(model_id)
update.multipart = true
create.multipart = true

model.active_storage_has_one_fields.each { |field| configure_active_storage_has_one_field(field.to_sym) }
model.active_storage_has_many_fields.each { |field| configure_active_storage_has_many_field(field.to_sym) }
model.active_storage_has_one_fields.each { |field| configure_active_storage_field(field.to_sym, :has_one) }
model.active_storage_has_many_fields.each { |field| configure_active_storage_field(field.to_sym, :has_many) }
end

private

def configure_active_storage_has_one_field(field)
def configure_active_storage_field(field, field_type)
columns << field
columns.exclude "#{field}_attachment".to_sym
columns.exclude "#{field}_blob".to_sym
columns[field].form_ui ||= :active_storage_has_one
columns[field].params.add "delete_#{field}"
end

def configure_active_storage_has_many_field(field)
columns << field
columns.exclude "#{field}_attachments".to_sym
columns.exclude "#{field}_blobs".to_sym
columns[field].form_ui ||= :active_storage_has_many
columns.exclude (field_type == :has_many ? "#{field}_attachments" : "#{field}_attachment").to_sym
columns.exclude (field_type == :has_many ? "#{field}_blobs" : "#{field}_blob").to_sym
columns[field].form_ui ||= "active_storage_#{field_type}".to_sym
columns[field].params.add "delete_#{field}"
end
end
Expand Down

0 comments on commit 91bbec7

Please sign in to comment.