Skip to content

Commit

Permalink
Feature/allow custom model names (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev authored Aug 25, 2020
1 parent 4c378cb commit 5238aaf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
12 changes: 2 additions & 10 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index
actions = []

if params[:resource_id].present?
model = resource_model.safe_constantize.find params[:resource_id]
model = resource_model.find params[:resource_id]
end

avo_actions.each do |action|
Expand All @@ -20,7 +20,7 @@ def index
end

def handle
models = resource_model.safe_constantize.find action_params[:resource_ids]
models = resource_model.find action_params[:resource_ids]
avo_action = action_params[:action_class].safe_constantize.new
avo_action.handle_action(request, models, action_params[:fields])

Expand All @@ -34,13 +34,5 @@ def handle
def action_params
params.permit(:resource_name, :action_id, :action_class, resource_ids: [], fields: {})
end

def resource_model
params[:resource_name].to_s.camelize.singularize
end

def avo_resource
App.get_resource resource_model
end
end
end
9 changes: 9 additions & 0 deletions app/controllers/avo/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ def exception_logger(exception)
}, status: ActionDispatch::ExceptionWrapper.status_code_for_exception(exception.class.name) }
end
end

private
def resource_model
avo_resource.model
end

def avo_resource
App.get_resource params[:resource_name].to_s.camelize.singularize
end
end
end
24 changes: 6 additions & 18 deletions app/controllers/avo/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def index
query = related_model.find(params[:via_resource_id]).public_send(params[:via_relationship])
params[:per_page] = Avo.configuration.via_per_page
elsif ['has_many', 'has_and_belongs_to_many'].include? params[:for_relation]
resources = resource_model.safe_constantize.all.map do |model|
resources = resource_model.all.map do |model|
{
value: model.id,
label: model.send(avo_resource.title),
Expand All @@ -28,7 +28,7 @@ def index
resources: resources
}
else
query = resource_model.safe_constantize
query = resource_model
end

query = query.order("#{params[:sort_by]} #{params[:sort_direction]}")
Expand Down Expand Up @@ -138,7 +138,7 @@ def create

casted_params = cast_nullable(create_params)

resource = resource_model.safe_constantize.new(casted_params)
resource = resource_model.new(casted_params)
resource.save!

render json: {
Expand All @@ -149,7 +149,7 @@ def create
end

def fields
resource = resource_model.safe_constantize.new
resource = resource_model.new

render json: {
resource: Avo::Resources::Resource.hydrate_resource(resource, avo_resource, :create),
Expand Down Expand Up @@ -219,23 +219,11 @@ def cast_nullable(params)

private
def resource
eager_load_files(resource_model.safe_constantize).find params[:id]
end

def resource_model
params[:resource_name].to_s.camelize.singularize
end

def avo_resource
App.get_resource resource_model
end

def resource_fields
avo_resource.get_fields
eager_load_files(resource_model).find params[:id]
end

def permitted_params
permitted = resource_fields.select(&:updatable).map do |field|
permitted = avo_resource.get_fields.select(&:updatable).map do |field|
# If it's a relation
if field.methods.include? :foreign_key
database_id = field.foreign_key(avo_resource.model)
Expand Down
6 changes: 6 additions & 0 deletions lib/avo/app/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def title
'id'
end

def model
return @model if @model.present?

underscore_name.to_s.camelize.singularize
end

def get_fields
self.class.get_fields
end
Expand Down
23 changes: 14 additions & 9 deletions spec/system/avo/code_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

RSpec.describe 'CodeField', type: :system do
describe 'without value' do
let!(:user) { create :user , custom_css: ''}
let!(:user) { create :user , custom_css: '' }

context 'show' do
it 'displays the projects empty custom_css (dash)' do
visit "/avo/resources/users/#{user.id}"
wait_for_loaded

expect(find_field_value_element('custom_css')).to have_text empty_dash
end
Expand All @@ -15,6 +16,7 @@
context 'edit' do
it 'has the projects custom_css label and empty code editor' do
visit "/avo/resources/users/#{user.id}/edit"
wait_for_loaded

custom_css_element = find_field_element('custom_css')

Expand All @@ -26,6 +28,7 @@

it 'change the projects custom_css code' do
visit "/avo/resources/users/#{user.id}/edit"
wait_for_loaded

fill_in_editor_field 'Hello World'

Expand All @@ -41,14 +44,6 @@
let!(:css) { '.input { background: #000000; }' }
let!(:user) { create :user , custom_css: css }

context 'show' do
it 'displays the projects custom_css' do
visit "/avo/resources/users/#{user.id}"

expect(page).to have_editor_display text: css
end
end

context 'edit' do
it 'has the projects custom_css label and filled code editor' do
visit "/avo/resources/users/#{user.id}/edit"
Expand All @@ -63,6 +58,7 @@

it 'change the projects custom_css code to another value' do
visit "/avo/resources/users/#{user.id}/edit"
wait_for_loaded

fill_in_editor_field '.input { background: #ffffff; }'

Expand All @@ -72,6 +68,15 @@
expect(page).to have_editor_display text: '.input { background: #ffffff; }'
end
end

context 'show' do
it 'displays the projects custom_css' do
visit "/avo/resources/users/#{user.id}"
wait_for_loaded

expect(page).to have_editor_display text: css
end
end
end

def fill_in_editor_field(text)
Expand Down

0 comments on commit 5238aaf

Please sign in to comment.