app/models/mega_bar/field.rb in mega_bar-0.0.1 vs app/models/mega_bar/field.rb in mega_bar-0.5.1
- old
+ new
@@ -1,31 +1,55 @@
module MegaBar
class Field < ActiveRecord::Base
- belongs_to :model
- has_many :field_display
- after_create :make_field_displays #, :only => [:create] #add update.
after_create :make_migration #, :only => [:create] #add update.
- after_save :make_field_displays
- attr_accessor :new_field_display, :edit_field_display, :index_field_display, :show_field_display
-
- #after_create :make_migration
- scope :by_model, ->(model_id) { where(model_id: model_id) if model_id.present? }
- def make_field_displays
- return if ENV['mega_bar_data_loading'] == 'yes'
- actions = []
- actions << {:format=>'textread', :action=>'index', :field_id=>self.id, :header=>self.field.pluralize} if (!FieldDisplay.by_fields(self.id).by_action('index').present? && @index_field_display == 'y')
- actions << {:format=>'textread', :action=>'show', :field_id=>self.id, :header=>self.field} if (!FieldDisplay.by_fields(self.id).by_action('show').present? && @show_field_display == 'y')
- actions << {:format=>'textbox', :action=>'new', :field_id=>self.id, :header=>self.field} if (!FieldDisplay.by_fields(self.id).by_action('new').present? && @new_field_display == 'y')
- actions << {:format=>'textbox', :action=>'edit', :field_id=>self.id, :header=>self.field} if (!FieldDisplay.by_fields(self.id).by_action('edit').present? && @edit_field_display == 'y')
- actions.each do | action |
- FieldDisplay.create(:field_id=>self.id, :format=>action[:format], :action=>action[:action], :header=>action[:header])
+ after_destroy :delete_field_displays
+ after_save :make_field_displays
+ attr_accessor :model_display_ids, :new_field_display, :edit_field_display, :index_field_display, :show_field_display, :block_id
+ before_create :standardize_tablename
+ belongs_to :model
+ has_many :options, dependent: :destroy
+ scope :by_model, ->(model_id) { where(model_id: model_id) if model_id.present? }
+ validate :table_exists, on: :create unless Rails.env.test?
+ validates_format_of :tablename, on: [:create, :update], :multiline => true, allow_nil: false, with: /[a-z]+/, message: 'no caps'
+ validates_presence_of :model_id, :tablename, :field, :default_data_format, :default_data_format_edit
+ validates_uniqueness_of :field, scope: :model_id, message: "dupe field for this model"
+
+ private
+
+ def table_exists
+ return true if self.tablename == 'accessor'
+ modle = Model.find(self.model_id) #this is a ugly dependency so this doesn't run in test environment.
+ prefix = modle.modyule.nil? || modle.modyule.empty? ? '' : modle.modyule.split('::').map { | m | m.underscore }.join('_') + '_'
+ self.tablename = prefix + self.tablename if prefix + self.tablename == modle.tablename
+ return true if ActiveRecord::Base.connection.table_exists? self.tablename
+ errors.add(:base, 'That table does not exist')
+ return false
+ end
+
+ def make_field_displays
+ return unless self.model_display_ids.present?
+ self.model_display_ids = self.model_display_ids.reject(&:blank?)
+ mds = ModelDisplay.find(self.model_display_ids) if self.model_display_ids.present?
+ return unless mds
+ mds.each do | md |
+ data_display = ['new', 'edit'].include?(md.action) ? self.default_data_format_edit : self.default_data_format
+ FieldDisplay.create(model_display_id: md.id, field_id: self.id, format:data_display, header: self.field.humanize)
end
end
+
+ def standardize_tablename
+
+ end
+
def make_migration
- return if ENV['mega_bar_data_loading'] == 'yes'
- return if Model.connection.column_exists?(self.tablename, self.field)
- system 'rails g mega_bar:mega_bar_fields ' + self.tablename + ' ' + self.field + ' ' + 'string'
+ return true if self.accessor == 'y'
+ return if Model.connection.column_exists?(self.tablename, self.field)
+ boolean = self.data_type == 'boolean' ? ' null: false, default: false' : '' #todo allow default true.
+ system 'bundle exec rails g mega_bar:mega_bar_fields ' + self.tablename + ' ' + self.field + ' ' + self.data_type + boolean
ActiveRecord::Migrator.migrate "db/migrate"
+ system 'rake db:schema:dump'
end
-
+ def delete_field_displays
+ FieldDisplay.by_fields(self.id).destroy_all
+ end
end
-end
\ No newline at end of file
+end