-
Notifications
You must be signed in to change notification settings - Fork 5
Add BetterTogether::Seed #790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rsmithlal
wants to merge
16
commits into
main
Choose a base branch
from
feature/the-seed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
204cfd6
Add BetterTogether::Seed
rsmithlal 3a39d88
Merge branch 'main' into feature/the-seed
rsmithlal 70401fd
Merge branch 'main' into feature/the-seed
rsmithlal 0519193
Add custom seed data config for BetterTogether::Wizard
rsmithlal c1b5d69
Merge branch 'main' into feature/the-seed
rsmithlal ce7f802
Merge branch 'main' into feature/the-seed
rsmithlal abcec97
Merge branch 'main' into feature/the-seed
rsmithlal c8f5384
Rubocop fixes
rsmithlal 5ef2113
Merge branch 'main' into feature/the-seed
rsmithlal b9d12a1
Add Seedable System Enhancement Implementation Plan
rsmithlal 12a0ad0
Implement security features for seed data handling, including file pa…
rsmithlal ac44bf0
Implement comprehensive security hardening for Seedable system, inclu…
rsmithlal 8b09a65
Rubocop fixes
rsmithlal 118f357
Add SeedPlanting model and integrate planting tracking into Seed impo…
rsmithlal 3dc9d38
Refactor seed import methods to enhance planting tracking; rename met…
rsmithlal 14e64d6
Refactor seed planting methods for improved validation and error hand…
rsmithlal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module BetterTogether | ||
| # CRUD for Seed records | ||
| class SeedsController < ApplicationController | ||
| before_action :set_seed, only: %i[show edit update destroy] | ||
|
|
||
| # GET /seeds | ||
| def index | ||
| @seeds = Seed.all | ||
| end | ||
|
|
||
| # GET /seeds/1 | ||
| def show; end | ||
|
|
||
| # GET /seeds/new | ||
| def new | ||
| @seed = Seed.new | ||
| end | ||
|
|
||
| # GET /seeds/1/edit | ||
| def edit; end | ||
|
|
||
| # POST /seeds | ||
| def create | ||
| @seed = Seed.new(seed_params) | ||
|
|
||
| if @seed.save | ||
| redirect_to @seed, notice: 'Seed was successfully created.' | ||
| else | ||
| render :new, status: :unprocessable_entity | ||
| end | ||
| end | ||
|
|
||
| # PATCH/PUT /seeds/1 | ||
| def update | ||
| if @seed.update(seed_params) | ||
| redirect_to @seed, notice: 'Seed was successfully updated.', status: :see_other | ||
| else | ||
| render :edit, status: :unprocessable_entity | ||
| end | ||
| end | ||
|
|
||
| # DELETE /seeds/1 | ||
| def destroy | ||
| @seed.destroy! | ||
| redirect_to seeds_url, notice: 'Seed was successfully destroyed.', status: :see_other | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Use callbacks to share common setup or constraints between actions. | ||
| def set_seed | ||
| @seed = Seed.find(params[:id]) | ||
| end | ||
|
|
||
| # Only allow a list of trusted parameters through. | ||
| def seed_params | ||
| params.fetch(:seed, {}) | ||
| end | ||
| end | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module BetterTogether | ||
| module SeedsHelper # rubocop:todo Style/Documentation | ||
| end | ||
| end |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module BetterTogether | ||
| # Allows for import and export of data in a structured and standardized way | ||
| class Seed < ApplicationRecord # rubocop:todo Metrics/ClassLength | ||
| self.table_name = 'better_together_seeds' | ||
| self.inheritance_column = :type # Defensive for STI safety | ||
|
|
||
| include Creatable | ||
| include Identifier | ||
| include Privacy | ||
|
|
||
| DEFAULT_ROOT_KEY = 'better_together' | ||
|
|
||
| # 1) Make sure you have Active Storage set up in your app | ||
| # This attaches a single YAML file to each seed record | ||
| has_one_attached :yaml_file | ||
|
|
||
| # 2) Polymorphic association: optional | ||
| belongs_to :seedable, polymorphic: true, optional: true | ||
|
|
||
| validates :type, :identifier, :version, :created_by, :seeded_at, | ||
| :description, :origin, :payload, presence: true | ||
|
|
||
| after_create_commit :attach_yaml_file | ||
| after_update_commit :attach_yaml_file | ||
|
|
||
| # ------------------------------------------------------------- | ||
| # Scopes | ||
| # ------------------------------------------------------------- | ||
| scope :by_type, ->(type) { where(type: type) } | ||
| scope :by_identifier, ->(identifier) { where(identifier: identifier) } | ||
| scope :latest_first, -> { order(created_at: :desc) } | ||
| scope :latest_version, ->(type, identifier) { by_type(type).by_identifier(identifier).latest_first.limit(1) } | ||
| scope :latest, -> { latest_first.limit(1) } | ||
|
|
||
| # ------------------------------------------------------------- | ||
| # Accessor overrides for origin/payload => Indifferent Access | ||
| # ------------------------------------------------------------- | ||
| def origin | ||
| super&.with_indifferent_access || {} | ||
| end | ||
|
|
||
| def payload | ||
| super&.with_indifferent_access || {} | ||
| end | ||
|
|
||
| # Helpers for nested origin data | ||
| def contributors | ||
| origin[:contributors] || [] | ||
| end | ||
|
|
||
| def platforms | ||
| origin[:platforms] || [] | ||
| end | ||
|
|
||
| # ------------------------------------------------------------- | ||
| # plant = internal DB creation (used by import) | ||
| # ------------------------------------------------------------- | ||
| def self.plant(type:, identifier:, version:, metadata:, content:) # rubocop:todo Metrics/MethodLength | ||
| create!( | ||
| type: type, | ||
| identifier: identifier, | ||
| version: version, | ||
| created_by: metadata[:created_by], | ||
| seeded_at: metadata[:created_at], | ||
| description: metadata[:description], | ||
| origin: metadata[:origin], | ||
| payload: content, | ||
| seedable_type: metadata[:seedable_type], | ||
| seedable_id: metadata[:seedable_id] | ||
| ) | ||
| end | ||
|
|
||
| # ------------------------------------------------------------- | ||
| # import = read a seed and store in DB | ||
| # ------------------------------------------------------------- | ||
| def self.import(seed_data, root_key: DEFAULT_ROOT_KEY) # rubocop:todo Metrics/MethodLength | ||
| data = seed_data.deep_symbolize_keys.fetch(root_key.to_sym) | ||
| metadata = data.fetch(:seed) | ||
| content = data.except(:version, :seed) | ||
|
|
||
| plant( | ||
| type: metadata.fetch(:type), | ||
| identifier: metadata.fetch(:identifier), | ||
| version: data.fetch(:version), | ||
| metadata: { | ||
| created_by: metadata.fetch(:created_by), | ||
| created_at: Time.iso8601(metadata.fetch(:created_at)), | ||
| description: metadata.fetch(:description), | ||
| origin: metadata.fetch(:origin), | ||
| seedable_type: metadata[:seedable_type], | ||
| seedable_id: metadata[:seedable_id] | ||
| }, | ||
| content: content | ||
| ) | ||
| end | ||
|
|
||
| # ------------------------------------------------------------- | ||
| # export = produce a structured hash including seedable info | ||
| # ------------------------------------------------------------- | ||
| # rubocop:todo Metrics/MethodLength | ||
| def export(root_key: DEFAULT_ROOT_KEY) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength | ||
| seed_obj = { | ||
| type: type, | ||
| identifier: identifier, | ||
| created_by: created_by, | ||
| created_at: seeded_at.iso8601, | ||
| description: description, | ||
| origin: origin.deep_symbolize_keys | ||
| } | ||
|
|
||
| # If seedable_type or seedable_id is present, include them | ||
| seed_obj[:seedable_type] = seedable_type if seedable_type.present? | ||
| seed_obj[:seedable_id] = seedable_id if seedable_id.present? | ||
|
|
||
| { | ||
| root_key => { | ||
| version: version, | ||
| seed: seed_obj, | ||
| **payload.deep_symbolize_keys | ||
| } | ||
| } | ||
| end | ||
| # rubocop:enable Metrics/MethodLength | ||
|
|
||
| # Export as YAML | ||
| def export_yaml(root_key: DEFAULT_ROOT_KEY) | ||
| export(root_key: root_key).deep_stringify_keys.to_yaml | ||
| end | ||
|
|
||
| # A recommended file name for the exported seed | ||
| def versioned_file_name | ||
| timestamp = seeded_at.utc.strftime('%Y%m%d%H%M%S') | ||
| "#{type.demodulize.underscore}_#{identifier}_v#{version}_#{timestamp}.yml" | ||
| end | ||
|
|
||
| # ------------------------------------------------------------- | ||
| # load_seed for file or named namespace | ||
| # ------------------------------------------------------------- | ||
| def self.load_seed(source, root_key: DEFAULT_ROOT_KEY) # rubocop:todo Metrics/MethodLength | ||
| # 1) Direct file path | ||
| if File.exist?(source) | ||
| begin | ||
| seed_data = YAML.load_file(source) | ||
| return import(seed_data, root_key: root_key) | ||
| rescue StandardError => e | ||
| raise "Error loading seed from file '#{source}': #{e.message}" | ||
| end | ||
| end | ||
|
|
||
| # 2) 'namespace' approach => config/seeds/#{source}.yml | ||
| path = Rails.root.join('config', 'seeds', "#{source}.yml").to_s | ||
| raise "Seed file not found for '#{source}' at path '#{path}'" unless File.exist?(path) | ||
|
|
||
| begin | ||
| seed_data = YAML.load_file(path) | ||
| import(seed_data, root_key: root_key) | ||
| rescue StandardError => e | ||
| raise "Error loading seed from namespace '#{source}' at path '#{path}': #{e.message}" | ||
| end | ||
| end | ||
|
|
||
| # ------------------------------------------------------------- | ||
| # Attach the exported YAML as an Active Storage file | ||
| # ------------------------------------------------------------- | ||
| def attach_yaml_file | ||
| yml_data = export_yaml | ||
| yaml_file.attach( | ||
| io: StringIO.new(yml_data), | ||
| filename: versioned_file_name, | ||
| content_type: 'text/yaml' | ||
| ) | ||
| end | ||
| end | ||
| end |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Brakeman
Possible unprotected redirect. Note