Created
September 15, 2016 12:59
-
-
Save markmcdonald51/070712f67418f74be5f9d03f1b3ea7ad to your computer and use it in GitHub Desktop.
meta mark sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'udt_model' | |
class MetaMark::ActiveRecordModel | |
extend UdtModel | |
def self.active_record_model_as_file(udt) | |
# return if File.exist?(udt.path_to_udt_model) | |
# m = MetaMark::ActiveRecordModel.new(udt) | |
table_name = udt.db_table_name | |
new_class_name = udt.udt_class_name # m.udt_class_name(table_name, udt) | |
poly = udt.udt_polymorphic_name | |
validations = add_validations(udt) | |
workflow = get_workflow_as_aasm(udt) | |
workflow = "\n include AASM\n #{workflow}\n" if workflow.present? | |
# this creates a recursion problem! | |
# udt.udt_associations.each do |a| | |
# puts a.udt.name | |
# assoc_model = self.active_record_model_as_file(a.udt) | |
# end | |
associations = create_associations(udt) | |
has_many_belongs_to = get_assoc_has_many_belongs_to(udt) | |
attachments = udt.udt_table_fields.active | |
.select { |f| f.udt_field.udt_field_type.name == 'attachment' }.map do |a| | |
"\n\t\t has_attached_file :\"ref_#{udt.id}_#{a.udt_field_id}\" | |
# validates :#{a.field_name} , attachment_presence: true | |
# validates_with AttachmentPresenceValidator, attributes: :#{a.field_name} | |
# validates_with AttachmentSizeValidator, attributes: :#{a.field_name} , less_than: 30.megabytes | |
do_not_validate_attachment_file_type :\"ref_#{udt.id}_#{a.udt_field_id}\" | |
" | |
end.join("\n") | |
association_label = udt.label_udt_fields.present? ? | |
udt.label_udt_fields.map { |f| %`\#{try(:#{f.field_name}) \}` }.join(' ') | |
: %(#{udt.name} \- \#{self.id.to_s} ) | |
model_class_text = %` | |
require 'meta_model_include' | |
\nclass #{new_class_name} < ActiveRecord::Base | |
include MetaModelInclude | |
self.table_name = "#{table_name}" | |
after_create :log_inital_aasm_status, if: Proc.new { |u| u.try(:aasm).present? } | |
has_paper_trail | |
has_many :aasm_status_changes, as: :aasm_loggable, dependent: :destroy | |
belongs_to :#{poly}, polymorphic: true | |
belongs_to :aasm_assignee, class_name: 'User' | |
belongs_to :aasm_assignor, class_name: 'User' | |
#{attachments} | |
#{associations} | |
#{has_many_belongs_to} | |
#{validations} | |
#{workflow} | |
define_method(:log_inital_aasm_status) do | |
aasm_status_changes.create( | |
user: current_user, | |
to_state: aasm.current_state, | |
event: 'start event') | |
end | |
define_method(:log_status_change) do | |
from_state = aasm.from_state | |
if last_rec = aasm_status_changes.where(to_state: from_state, end_timestamp: nil). | |
order(created_at: :desc).first | |
last_rec.update(end_timestamp: Time.now) | |
end | |
aasm_status_changes.create!( | |
from_state: from_state, | |
user: current_user, | |
to_state: aasm.to_state, | |
event: aasm.current_event) | |
end | |
# define_method(:label) { send(udt.udt_fields.first.field_name) rescue nil } | |
define_method(:label) do | |
" #{association_label}" rescue nil | |
end | |
define_method(:to_system_model) do | |
Udt.where(db_table_name: self.class.table_name).take! | |
end | |
alias_method :udt, :to_system_model | |
end # END OF MODEL | |
` | |
file_name = udt.path_to_udt_model | |
dir = Rails.root.join(*%w(app models org) + ["org#{udt.organization.id}"]) | |
response = FileUtils.mkdir_p(dir) | |
File.open(file_name, 'w') do |f| | |
puts 'writing file' | |
f.puts model_class_text | |
end | |
model_class_text | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment