lib/generators/effective/helpers.rb in effective_developer-0.5.5 vs lib/generators/effective/helpers.rb in effective_developer-0.6.0

- old
+ new

@@ -2,10 +2,28 @@ module Generators module Helpers protected + # This is kind of a validate for the resource + def resource_valid? + if resource.klass.blank? + say_status(:error, "Unable to find resource klass from #{name}", :red) + return false + end + + true + end + + def with_resource_tenant(&block) + if defined?(Tenant) && resource.tenant.present? + Tenant.as(resource.tenant) { yield } + else + yield + end + end + def resource @resource ||= Effective::Resource.new(name) end def crud_actions @@ -53,27 +71,29 @@ def invokable(attributes) attributes.map { |name, (type, _)| "#{name}:#{type}" } end def resource_attributes(all: false) - klass_attributes = resource.klass_attributes(all: all) + with_resource_tenant do + klass_attributes = resource.klass_attributes(all: all) - if klass_attributes.blank? - if ActiveRecord::Migration.respond_to?(:check_pending!) - pending = (ActiveRecord::Migration.check_pending! rescue true) - else - pending = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_paths)).pending_migrations.present? - end + if klass_attributes.blank? + if ActiveRecord::Migration.respond_to?(:check_pending!) + pending = (ActiveRecord::Migration.check_pending! rescue true) + else + pending = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_paths)).pending_migrations.present? + end - if pending - migrate = ask("Unable to read the attributes of #{resource.klass || resource.name}. There are pending migrations. Run db:migrate now? [y/n]") - system('bundle exec rake db:migrate') if migrate.to_s.include?('y') + if pending + migrate = ask("Unable to read the attributes of #{resource.klass || resource.name}. There are pending migrations. Run db:migrate now? [y/n]") + system('bundle exec rake db:migrate') if migrate.to_s.include?('y') + end + + klass_attributes = resource.klass_attributes(all: all) end - klass_attributes = resource.klass_attributes(all: all) + klass_attributes.presence || resource.model_attributes(all: all) end - - klass_attributes.presence || resource.model_attributes(all: all) end end end end