Skip to content

Commit

Permalink
add api actions migration
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Jan 12, 2022
1 parent 45f3a17 commit 909d2dd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ GEM
mini_mime (1.1.2)
mini_portile2 (2.7.1)
minitest (5.15.0)
motor-admin (0.2.53)
motor-admin (0.2.56)
activerecord-filter (>= 5.2)
ar_lazy_preload (~> 0.6)
audited (~> 5.0)
Expand Down
81 changes: 81 additions & 0 deletions db/migrate/20220112095600_upgrade_motor_api_actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/BlockLength, Naming/MethodName, Naming/VariableName
class UpgradeMotorApiActions < ActiveRecord::Migration[7.0]
def _MotorApiConfig
@_MotorApiConfig ||= Class.new(ActiveRecord::Base) do
self.table_name = 'motor_api_configs'

encrypts :credentials

serialize :credentials, Motor::HashSerializer
serialize :preferences, Motor::HashSerializer

attribute :preferences, default: -> { HashWithIndifferentAccess.new }
attribute :credentials, default: -> { HashWithIndifferentAccess.new }
end
end

def _MotorForm
@_MotorForm ||= Class.new(ActiveRecord::Base) do
self.table_name = 'motor_forms'

serialize :preferences, Motor::HashSerializer
end
end

def _MotorResource
@_MotorResource ||= Class.new(ActiveRecord::Base) do
self.table_name = 'motor_resources'

serialize :preferences, Motor::HashSerializer
end
end

def up
_MotorResource.all.each do |resource|
resource.preferences.fetch(:actions, []).each do |action|
next unless action[:action_type] == 'api'

api_path = action[:preferences][:api_path]
api_url = api_path[%r{\Ahttps?://[^/]+}]
api_path = api_path.delete_prefix(api_url).sub(%r{\A/?}, '/') if api_url

api_config =
if api_url
_MotorApiConfig.create_with(url: api_url).find_or_create_by!(name: api_url.sub(%r{\Ahttps?://}))
else
_MotorApiConfig.create_with(url: '/').find_or_create_by!(name: 'origin')
end

resource_display_name = (resource[:preferences][:display_name] || resource.name).titleize.singularize

form = _MotorForm.create_with(
api_config_name: api_config.name,
api_path: api_path.sub(/\{{1,2}\w+\}{1,2}/, '{{id}}'),
http_method: 'POST',
preferences: {
fields: [{
name: 'id',
display_name: resource_display_name,
default_value: '',
field_type: 'reference',
reference: { model_name: resource.name },
validators: [{ required: true }]
}]
}
).find_or_create_by!(
name: "#{action[:display_name]} #{resource_display_name}"
)

action[:action_type] = 'form'
action[:preferences] = { form_id: form.id }
end

resource.save!
end
end

def down; end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/BlockLength, Naming/MethodName, Naming/VariableName

0 comments on commit 909d2dd

Please sign in to comment.