Skip to content

Commit

Permalink
Merge branch 'main' into detach-through-associations
Browse files Browse the repository at this point in the history
  • Loading branch information
binarygit authored Jul 25, 2024
2 parents bd7251a + 3bce3ba commit e608c8f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
30 changes: 20 additions & 10 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,29 @@ def new
end

def create
association_name = BaseResource.valid_association_name(@record, association_from_params)

if reflection_class == "HasManyReflection"
@record.send(association_name) << @attachment_record
else
@record.send(:"#{association_name}=", @attachment_record)
respond_to do |format|
if create_association
format.html {
redirect_back fallback_location: resource_view_response_path,
notice: t("avo.attachment_class_attached", attachment_class: @related_resource.name)
}
else
format.turbo_stream {
render turbo_stream: turbo_stream.append("alerts", partial: "avo/partials/all_alerts")
}
end
end
end

respond_to do |format|
if @record.save
format.html { redirect_back fallback_location: resource_view_response_path, notice: t("avo.attachment_class_attached", attachment_class: @related_resource.name) }
def create_association
association_name = BaseResource.valid_association_name(@record, association_from_params)

perform_action_and_record_errors do
if reflection_class == "HasManyReflection"
@record.send(association_name) << @attachment_record
else
format.html { render :new }
@record.send(:"#{association_name}=", @attachment_record)
@record.save!
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,12 @@ def destroy_record_action
def perform_action_and_record_errors(&block)
begin
succeeded = block.call
rescue ActiveRecord::RecordInvalid => e
rescue ActiveRecord::RecordInvalid => error
# Do nothing as the record errors are already being displayed
# On associations controller add errors from join record to record
if controller_name == "associations"
@record.errors.add(:base, error.message)
end
rescue => exception
# In case there's an error somewhere else than the record
# Example: When you save a license that should create a user for it and creating that user throws and error.
Expand Down
15 changes: 14 additions & 1 deletion lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def boot
def init
Avo::Current.error_manager = Avo::ErrorManager.build
# Check rails version issues only on NON Production environments
check_rails_version_issues unless Rails.env.production?
unless Rails.env.production?
check_rails_version_issues
display_menu_editor_warning
end
Avo::Current.resource_manager = Avo::Resources::ResourceManager.build
Avo::Current.tool_manager = Avo::Tools::ToolManager.build

Expand Down Expand Up @@ -160,6 +163,16 @@ def check_rails_version_issues
})
end
end

def display_menu_editor_warning
if Avo.configuration.license == "community" && has_main_menu?
Avo.error_manager.add({
url: "https://docs.avohq.io/3.0/menu-editor.html",
target: "_blank",
message: "The menu editor is available exclusively with the Pro license or above. Consider upgrading to access this feature."
})
end
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/models/team_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class TeamMembership < ApplicationRecord
belongs_to :user
before_destroy :raise_test_error, if: -> { Rails.env.test? }

validate :fail, if: -> { ENV["MEMBERSHIP_FAIL"] == "true" }

def fail
errors.add(:team_membership, "dummy fail.")
end

def name
"#{team&.name} - #{user&.name}"
end
Expand Down
1 change: 0 additions & 1 deletion spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class User < ApplicationRecord
has_many :team_memberships
has_many :teams, through: :team_memberships


has_one_attached :cv

friendly_id :name, use: :slugged
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"
require "rails/generators"

RSpec.feature "ResourceMissingModels", type: :feature do
RSpec.feature "AvoWarning", type: :feature do
before :all do
Rails::Generators.invoke("avo:resource", ["bad", "--quiet", "--skip"], {destination_root: Rails.root})
end
Expand All @@ -13,12 +13,19 @@
end
end

it "tests the message" do
it "displays bad model warning" do
visit "/admin/resources/comments"

click_on_sidebar_item "Fish"
expect(page).to have_text "Avo::Resources::Bad does not have a valid model assigned. It failed to find the Bad model."
expect(page).to have_text "Please create that model or assign one using self.model_class = YOUR_MODEL"
expect(page).to have_link href: "https://docs.avohq.io/3.0/resources.html#self_model_class"
end

it "displays menu editor warning" do
visit "/admin/resources/comments"

expect(page).to have_text "The menu editor is available exclusively with the Pro license or above. Consider upgrading to access this feature."
expect(page).to have_link href: "https://docs.avohq.io/3.0/menu-editor.html"
end
end
30 changes: 30 additions & 0 deletions spec/system/avo/attach_fail_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Attachment fail", type: :system do
let!(:user) { create :user }
let!(:team) { create :team, name: "Haha team" }

it "if attach persist" do
ENV["MEMBERSHIP_FAIL"] = "true"

visit avo.resources_user_path(user, "tab-group_3" => "Teams")

scroll_to first_tab_group

click_on "Attach team"

expect(page).to have_text "Choose team"
expect(page).to have_select "fields_related_id"

select team.name, from: "fields_related_id"

click_on "Attach"
expect(page).to have_text "Validation failed: Team membership dummy fail."

ENV["MEMBERSHIP_FAIL"] = "false"
click_on "Attach"
expect(page).to have_text "Team attached."
end
end
2 changes: 1 addition & 1 deletion spec/system/avo/tabs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

expect(teams_frame).to have_text "Teams"
expect(teams_frame).to have_link "Attach team"
expect(teams_frame).to have_link "Create new team", href: "/admin/resources/teams/new?via_record_id=#{user.slug}&via_relation=users&via_relation_class=User&via_resource_class=Avo%3A%3AResources%3A%3AUser"
expect(teams_frame).to have_link "Create new team", href: "/admin/resources/teams/new?via_record_id=#{user.slug}&via_relation=admin&via_relation_class=User&via_resource_class=Avo%3A%3AResources%3A%3AUser"
end

it "hides the birthday tab" do
Expand Down

0 comments on commit e608c8f

Please sign in to comment.