【问题标题】:How can I "pretty print" comparisons between ruby hashes so a user can easily see the differences between them in a rails app?如何“漂亮地打印”ruby 哈希之间的比较,以便用户可以在 rails 应用程序中轻松查看它们之间的差异?
【发布时间】:2020-09-25 16:33:47
【问题描述】:

我的 Rails 应用程序中有一个版本历史记录功能,它通过将模型当时的 as_json 表示保存到数据库列来记录活动记录模型的快照。

模型数据是关系型的,有几个嵌套的has_many 关联也需要显示给用户。

我现在想为用户提供一种简单的方法来比较版本,类似于您在 Wordpress 或 Github 中可以找到的方法。 (example image)

我发现 this gem 效果很好,但它只接受字符串作为输入。

我无法保证给定对象中会出现哪些顶级键和嵌套键。

所以,我想我想做的是:

  1. 将存储的对象重构为 Ruby 哈希(已经完成)
  2. 将对象的存储版本打印为 YAML,使用换行符和制表符使用户可以理解
  3. 将该字符串输入到 diffy gem 中,并将其与不同版本的相同字符串进行比较。

或者,我可以将模型显示为 HTML(简单),并以某种方式将该 HTML 与 HTML 进行比较(看起来很难,似乎没有差异化的宝石可以做到这一点,并且手动计算似乎超出了我的能力)。

仅打印出散列的 to_yaml 表示会产生不带制表符或换行符的丑陋 YAML 输出,并且当我尝试通过 YAML 验证器运行它时验证失败。不知道为什么会这样。

我正在使用的数据类型示例。注意嵌套的关联数据。

 {"id"=>6, "name"=>"Abbey View Primary Academy", "organisation_id"=>3, "description"=>"Abbey View Primary Academy will be committed to delivering inspirational learning opportunities that are fun, purposeful and challenging. We want our children not just to achieve, but to exceed expectations and attain the very best results they can academically, creatively and personally. Our pupils will take ownership of their learning, becoming highly aspirational and resilient learners who are able to make positive contributions to life in modern Britain.\r\nOur pupils will be at the heart of every decision we make and we will offer a safe, nurturing and creative learning environment, which will enable us to foster a passion for learning and curiosity in a happy and supported way.", "url"=>"http://www.abbeyviewprimaryacademy.org/home", "visible"=>true, "email"=>"office@avpa.school", "visible_from"=>nil, "visible_to"=>nil, "created_at"=>"2020-06-04T17:09:13.965Z", "updated_at"=>"2020-06-06T14:00:21.420Z", "discarded_at"=>nil, "approved"=>true, "notes_count"=>0, "ofsted_reference_number"=>nil, "old_ofsted_external_id"=>nil, "label_list"=>[], "taxonomies"=>[{"id"=>318, "name"=>"Education and learning", "created_at"=>"2020-06-04T17:50:16.033Z", "updated_at"=>"2020-06-04T17:50:16.033Z", "parent_id"=>1, "locked"=>nil, "sort_order"=>4}, {"id"=>319, "name"=>"Primary schools", "created_at"=>"2020-06-04T17:50:16.108Z", "updated_at"=>"2020-06-04T17:50:16.108Z", "parent_id"=>318, "locked"=>nil, "sort_order"=>0}, {"id"=>255, "name"=>"Communication and Interaction", "created_at"=>"2020-06-04T17:09:13.793Z", "updated_at"=>"2020-06-04T17:09:13.793Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>3}, {"id"=>254, "name"=>"Hearing", "created_at"=>"2020-06-04T17:09:13.766Z", "updated_at"=>"2020-06-04T17:09:13.766Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>2}, {"id"=>253, "name"=>"Social, Emotional and Mental Health Difficulties", "created_at"=>"2020-06-04T17:09:13.728Z", "updated_at"=>"2020-06-04T17:09:13.728Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>1}, {"id"=>252, "name"=>"Autism", "created_at"=>"2020-06-04T17:09:13.661Z", "updated_at"=>"2020-06-04T17:09:13.661Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>0}], "locations"=>[{"id"=>6, "name"=>"Abbey View Primary Academy", "description"=>nil, "latitude"=>"51.616709", "longitude"=>"-0.745362", "address_1"=>" Kennedy Avenue", "city"=>"High Wycombe", "state_province"=>"Buckinghamshire", "postal_code"=>"HP11 1BX", "country"=>"GB", "google_place_id"=>nil}], "contacts"=>[{"service_id"=>6, "id"=>6, "name"=>"Abbey View Primary Academy", "title"=>"", "visible"=>false}], "cost_options"=>[], "regular_schedules"=>[]} 

【问题讨论】:

    标签: ruby-on-rails ruby yaml diff


    【解决方案1】:

    你可以使用JSON.pretty_generate:

    hash = {"id"=>6, "name"=>"Abbey View Primary Academy", "organisation_id"=>3, "description"=>"Abbey View Primary Academy will be committed to delivering inspirational learning opportunities that are fun, purposeful and challenging. We want our children not just to achieve, but to exceed expectations and attain the very best results they can academically, creatively and personally. Our pupils will take ownership of their learning, becoming highly aspirational and resilient learners who are able to make positive contributions to life in modern Britain.\r\nOur pupils will be at the heart of every decision we make and we will offer a safe, nurturing and creative learning environment, which will enable us to foster a passion for learning and curiosity in a happy and supported way.", "url"=>"http://www.abbeyviewprimaryacademy.org/home", "visible"=>true, "email"=>"office@avpa.school", "visible_from"=>nil, "visible_to"=>nil, "created_at"=>"2020-06-04T17:09:13.965Z", "updated_at"=>"2020-06-06T14:00:21.420Z", "discarded_at"=>nil, "approved"=>true, "notes_count"=>0, "ofsted_reference_number"=>nil, "old_ofsted_external_id"=>nil, "label_list"=>[], "taxonomies"=>[{"id"=>318, "name"=>"Education and learning", "created_at"=>"2020-06-04T17:50:16.033Z", "updated_at"=>"2020-06-04T17:50:16.033Z", "parent_id"=>1, "locked"=>nil, "sort_order"=>4}, {"id"=>319, "name"=>"Primary schools", "created_at"=>"2020-06-04T17:50:16.108Z", "updated_at"=>"2020-06-04T17:50:16.108Z", "parent_id"=>318, "locked"=>nil, "sort_order"=>0}, {"id"=>255, "name"=>"Communication and Interaction", "created_at"=>"2020-06-04T17:09:13.793Z", "updated_at"=>"2020-06-04T17:09:13.793Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>3}, {"id"=>254, "name"=>"Hearing", "created_at"=>"2020-06-04T17:09:13.766Z", "updated_at"=>"2020-06-04T17:09:13.766Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>2}, {"id"=>253, "name"=>"Social, Emotional and Mental Health Difficulties", "created_at"=>"2020-06-04T17:09:13.728Z", "updated_at"=>"2020-06-04T17:09:13.728Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>1}, {"id"=>252, "name"=>"Autism", "created_at"=>"2020-06-04T17:09:13.661Z", "updated_at"=>"2020-06-04T17:09:13.661Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>0}], "locations"=>[{"id"=>6, "name"=>"Abbey View Primary Academy", "description"=>nil, "latitude"=>"51.616709", "longitude"=>"-0.745362", "address_1"=>" Kennedy Avenue", "city"=>"High Wycombe", "state_province"=>"Buckinghamshire", "postal_code"=>"HP11 1BX", "country"=>"GB", "google_place_id"=>nil}], "contacts"=>[{"service_id"=>6, "id"=>6, "name"=>"Abbey View Primary Academy", "title"=>"", "visible"=>false}], "cost_options"=>[], "regular_schedules"=>[]}
    
    require 'json'
    
    JSON.pretty_generate(hash)
    #=> {
    #     "id": 6,
    #     "name": "Abbey View Primary Academy",
    #     "organisation_id": 3,
    #     "description": "Abbey View Primary Academy will be committed to delivering inspirational learning opportunities that are fun, purposeful and challenging. We want our children not just to achieve, but to exceed expectations and attain the very best results they can academically, creatively and personally. Our pupils will take ownership of their learning, becoming highly aspirational and resilient learners who are able to make positive contributions to life in modern Britain.\r\nOur pupils will be at the heart of every decision we make and we will offer a safe, nurturing and creative learning environment, which will enable us to foster a passion for learning and curiosity in a happy and supported way.",
    #     "url": "http://www.abbeyviewprimaryacademy.org/home",
    #     "visible": true,
    #     "email": "office@avpa.school",
    #     "visible_from": null,
    #     "visible_to": null,
    #     "created_at": "2020-06-04T17:09:13.965Z",
    #     "updated_at": "2020-06-06T14:00:21.420Z",
    #     "discarded_at": null,
    #     "approved": true,
    #     "notes_count": 0,
    #     "ofsted_reference_number": null,
    #     "old_ofsted_external_id": null,
    #     "label_list": [
    #   
    #     ],
    #     "taxonomies": [
    #       {
    #         "id": 318,
    #         "name": "Education and learning",
    #         "created_at": "2020-06-04T17:50:16.033Z",
    #         "updated_at": "2020-06-04T17:50:16.033Z",
    #         "parent_id": 1,
    #         "locked": null,
    #         "sort_order": 4
    #       },
    #       {
    #         "id": 319,
    #         "name": "Primary schools",
    # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-13
      • 2010-12-06
      • 1970-01-01
      • 2010-11-02
      • 2018-03-26
      • 2012-12-28
      • 1970-01-01
      • 2018-08-14
      相关资源
      最近更新 更多