Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active April 28, 2017 10:20
Show Gist options
  • Save elvisgiv/740ac6f31a87fe3d4703713dfb2ac5f7 to your computer and use it in GitHub Desktop.
Save elvisgiv/740ac6f31a87fe3d4703713dfb2ac5f7 to your computer and use it in GitHub Desktop.

manage model field with data stored on disk

../lib/gexcore/dashboards/service.rb

module Gexcore::Dashboards
  class Service < Gexcore::BaseService

    def self.load_dashboard(row)
      f = filename_dashboard(row.cluster, row.name)
      data =YAML.load_file(f)
      #
      data
    end

    ###
    def self.base_dir_dashboards
      File.join(Rails.root, "data/dashboards")
    end

    def self.filename_dashboard(cluster, name)
      File.join(base_dir_dashboards, "#{cluster.id}", "#{name}.yml")
    end

    def self.filename(dashboard)
      File.join(base_dir_dashboards, "#{dashboard.cluster_id}", "#{dashboard.name}.yml")
    end
  end
end
../app/models/dashboard.rb

class Dashboard < ActiveRecord::Base
  belongs_to :cluster
  ...
  ### content
  def fullpath
    Gexcore::Dashboards::Service.filename(self)
  end

  def dir_path
    File.join(Rails.root, "data/dashboards", "#{self.cluster_id}")
  end

  def content
    filename =  fullpath
    return nil if filename.nil?
    return '' if !File.exists? filename
    File.read(filename)
  end

  def content=(v)
    # create directory and file if not exist
    FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
    #work
    File.open(fullpath, "w+") do |f|
      f.write(v)
    end
  end
end
../app/controllers/admin/dashboards_controller.rb

class Admin::DashboardsController < Admin::MyAdminBaseController
  ...
private
  def dashboards_params
    params.require(:dashboard).permit(:name, :enabled, :title, :cluster_id, :pos, :content)
  end
end
../app/views/admin/dashboards/_form.html.haml

#mainform
  = horizontal_simple_form_for([:admin, @item], :html => { class: 'form-horizontalc', :multipart => true }) do |f|
    = render 'shared/form_errors', f: f

    = f.input :title
    = f.input :name
    = f.input :cluster_id, :value => @cluster_id, :readonly => true
    = f.input :pos, as: :integer
    = f.input :enabled, as: :boolean
    = f.input :content, as: :text, label: "Content", :rows => 16, :cols => 80, :data=>{}, input_html: {value: @item.content, class: 'form_input_content' }
    %div{id: "content", class: '', data: {}}(style="width:100%; height: 600px; border: 2px #333333 solid;")
    %br
    = f.button :submit, :class => "btn btn-primary"

= render 'js_ace'
:javascript
    function init_editor(id){
      //var editor = ace.edit("content");
      var editor = ace.edit(id);
      editor.setTheme("ace/theme/chrome");
      editor.getSession().setMode("ace/mode/haml");
      editor.getSession().setNewLineMode("windows");
      editor.getSession().setTabSize(2);
      return editor;
    }
    // init editors
    var editor = init_editor("content");

    $(document).ready(function() {

      // set content from input to editor
      $( "textarea.form_input_content" ).each(function( index ) {
        var textarea = $(this);
        textarea.hide();
        editor.getSession().setValue(textarea.val());
      });
      // set content from editor back to input
      $('#mainform form').submit(function(){
        $( "textarea.form_input_content" ).each(function( index ) {
          var textarea = $(this);
          textarea.val(editor.getSession().getValue());
        });
      });
    });
../app/views/admin/dashboards/_js_ace.html.haml

<script src="/js/ace/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment