Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. # Создаем/изменяем файл, содержащий что-либо на диске.
  2.  
  3. ```ruby
  4. ../lib/gexcore/dashboards/service.rb
  5.  
  6. module Gexcore::Dashboards
  7. class Service < Gexcore::BaseService
  8.  
  9. def self.load_dashboard(row)
  10. f = filename_dashboard(row.cluster, row.name)
  11. data =YAML.load_file(f)
  12. #
  13. data
  14. end
  15.  
  16. ###
  17. def self.base_dir_dashboards
  18. File.join(Rails.root, "data/dashboards")
  19. end
  20.  
  21. def self.filename_dashboard(cluster, name)
  22. File.join(base_dir_dashboards, "#{cluster.id}", "#{name}.yml")
  23. end
  24.  
  25. def self.filename(dashboard)
  26. File.join(base_dir_dashboards, "#{dashboard.cluster_id}", "#{dashboard.name}.yml")
  27. end
  28. end
  29. end
  30. ```
  31. ```ruby
  32. ../app/models/dashboard.rb
  33.  
  34. class Dashboard < ActiveRecord::Base
  35. belongs_to :cluster
  36. ...
  37. ### content
  38. def fullpath
  39. Gexcore::Dashboards::Service.filename(self)
  40. end
  41.  
  42. def dir_path
  43. File.join(Rails.root, "data/dashboards", "#{self.cluster_id}")
  44. end
  45.  
  46. def content
  47. filename = fullpath
  48. return nil if filename.nil?
  49. return '' if !File.exists? filename
  50. File.read(filename)
  51. end
  52.  
  53. def content=(v)
  54. # create directory and file if not exist
  55. FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
  56. #work
  57. File.open(fullpath, "w+") do |f|
  58. f.write(v)
  59. end
  60. end
  61. end
  62. ```
  63. ```ruby
  64. ../app/controllers/admin/dashboards_controller.rb
  65.  
  66. class Admin::DashboardsController < Admin::MyAdminBaseController
  67. ...
  68. private
  69. def dashboards_params
  70. params.require(:dashboard).permit(:name, :enabled, :title, :cluster_id, :pos, :content)
  71. end
  72. end
  73. ```
  74.  
  75. ```ruby
  76. ../app/views/admin/dashboards/_form.html.haml
  77.  
  78. #mainform
  79. = horizontal_simple_form_for([:admin, @item], :html => { class: 'form-horizontalc', :multipart => true }) do |f|
  80. = render 'shared/form_errors', f: f
  81.  
  82. = f.input :title
  83. = f.input :name
  84. = f.input :cluster_id, :value => @cluster_id, :readonly => true
  85. = f.input :pos, as: :integer
  86. = f.input :enabled, as: :boolean
  87. = f.input :content, as: :text, label: "Content", :rows => 16, :cols => 80, :data=>{}, input_html: {value: @item.content, class: 'form_input_content' }
  88. %div{id: "content", class: '', data: {}}(style="width:100%; height: 600px; border: 2px #333333 solid;")
  89. %br
  90. = f.button :submit, :class => "btn btn-primary"
  91.  
  92. = render 'js_ace'
  93. :javascript
  94. function init_editor(id){
  95. //var editor = ace.edit("content");
  96. var editor = ace.edit(id);
  97. editor.setTheme("ace/theme/chrome");
  98. editor.getSession().setMode("ace/mode/haml");
  99. editor.getSession().setNewLineMode("windows");
  100. editor.getSession().setTabSize(2);
  101. return editor;
  102. }
  103. // init editors
  104. var editor = init_editor("content");
  105.  
  106. $(document).ready(function() {
  107.  
  108. // set content from input to editor
  109. $( "textarea.form_input_content" ).each(function( index ) {
  110. var textarea = $(this);
  111. textarea.hide();
  112. editor.getSession().setValue(textarea.val());
  113. });
  114. // set content from editor back to input
  115. $('#mainform form').submit(function(){
  116. $( "textarea.form_input_content" ).each(function( index ) {
  117. var textarea = $(this);
  118. textarea.val(editor.getSession().getValue());
  119. });
  120. });
  121. });
  122. ```
  123. ```ruby
  124. ../app/views/admin/dashboards/_js_ace.html.haml
  125.  
  126. <script src="/js/ace/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
  127. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement