Skip to content

Instantly share code, notes, and snippets.

@mikker
Last active April 1, 2025 20:22
Show Gist options
  • Select an option

  • Save mikker/c435fc6bfcbdf67b60cd12ff96a45ee5 to your computer and use it in GitHub Desktop.

Select an option

Save mikker/c435fc6bfcbdf67b60cd12ff96a45ee5 to your computer and use it in GitHub Desktop.

Revisions

  1. mikker revised this gist Apr 1, 2025. 2 changed files with 18 additions and 9 deletions.
    3 changes: 2 additions & 1 deletion application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,8 @@ def bucket_options
    {
    publishable_key: Rails.application.credentials.bucket_publishable_key,
    user: { id: current_user&.id },
    company: { id: current_company&.id } # or current_user.id again if you don't have companies
    company: { id: current_company&.id }, # or current_user.id again if you don't have companies
    toolbar: Rails.env.development? || current_user&.admin?
    }
    end
    end
    24 changes: 16 additions & 8 deletions bucket_controller.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    import { Controller } from "@hotwired/stimulus";
    import { BucketClient } from "@bucketco/browser-sdk";

    const bucket = new BucketClient(window.BUCKET_OPTS);
    const bucket = new BucketClient({
    ...window.BUCKET_OPTS,
    host: "https://front-eu.bucket.co",
    });

    const prom = bucket.initialize();

    @@ -10,10 +13,12 @@ export default class extends Controller {
    feature: String,
    enabled: { type: Boolean, default: false },
    hideIfDisabled: { type: Boolean, default: true },
    hiddenClass: { type: String, default: "hidden" },
    };

    async connect() {
    this.initialDisplay = getComputedStyle(this.element).display
    this.toggleVisibility()

    await prom;

    this.update();
    @@ -25,25 +30,28 @@ export default class extends Controller {
    removeEventListener("turbo:load", this.update);
    }

    update() {
    update = () => {
    this.enabledValue = bucket.getFeature(this.featureValue).isEnabled;
    }
    };

    enabledValueChanged = () => {
    // Skip initial toggle so we can get initial display value before hiding
    if (!this.initialDisplay) return;

    if (this.hideIfDisabledValue) {
    this.toggleVisibility();
    }
    };

    toggleVisibility() {
    toggleVisibility = () => {
    if (!this.hideIfDisabledValue) return;

    if (this.enabledValue) {
    this.element.classList.remove(this.hiddenClassValue);
    this.element.style.display = this.initialDisplay
    } else {
    this.element.classList.add(this.hiddenClassValue);
    this.element.style.display = "none"
    }
    }
    };

    async track() {
    await prom;
  2. mikker revised this gist Sep 5, 2024. 1 changed file with 37 additions and 10 deletions.
    47 changes: 37 additions & 10 deletions bucket_controller.js
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,52 @@
    import { Controller } from "@hotwired/stimulus";
    import { BucketClient } from "@bucketco/browser-sdk";

    const bucket = new BucketClient(window.BUCKET_OPTS)
    const bucket = new BucketClient(window.BUCKET_OPTS);

    const prom = bucket.initialize().then(() => {
    // console.log("Bucket initialized");
    });
    const prom = bucket.initialize();

    export default class extends Controller {
    static values = { feature: String };
    static values = {
    feature: String,
    enabled: { type: Boolean, default: false },
    hideIfDisabled: { type: Boolean, default: true },
    hiddenClass: { type: String, default: "hidden" },
    };

    async connect() {
    await prom;
    bucket.getFeature(this.featureValue).isEnabled;

    this.update();

    addEventListener("turbo:load", this.update);
    }

    disconnect() {
    removeEventListener("turbo:load", this.update);
    }

    update() {
    this.enabledValue = bucket.getFeature(this.featureValue).isEnabled;
    }

    enabledValueChanged = () => {
    if (this.hideIfDisabledValue) {
    this.toggleVisibility();
    }
    };

    toggleVisibility() {
    if (!this.hideIfDisabledValue) return;

    if (this.enabledValue) {
    this.element.classList.remove(this.hiddenClassValue);
    } else {
    this.element.classList.add(this.hiddenClassValue);
    }
    }

    async track() {
    await prom;
    bucket.track(this.featureValue);
    }
    }

    // Don't forget in application.js
    // + application.register('bucket', BucketController)
    }
  3. mikker created this gist Sep 5, 2024.
    12 changes: 12 additions & 0 deletions application.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!doctype html>
    <html>
    <head>
    <script>
    window.BUCKET_OPTIONS = <%= bucket_options.to_json.html_safe %>
    </script>
    <%= javascript_include_tag :application %>
    </head>
    <body>
    <!-- ... -->
    </body>
    </html>
    15 changes: 15 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    class ApplicationController < ActionController::Base
    # ...

    helper_method :bucket_options

    private

    def bucket_options
    {
    publishable_key: Rails.application.credentials.bucket_publishable_key,
    user: { id: current_user&.id },
    company: { id: current_company&.id } # or current_user.id again if you don't have companies
    }
    end
    end
    25 changes: 25 additions & 0 deletions bucket_controller.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    import { Controller } from "@hotwired/stimulus";
    import { BucketClient } from "@bucketco/browser-sdk";

    const bucket = new BucketClient(window.BUCKET_OPTS)

    const prom = bucket.initialize().then(() => {
    // console.log("Bucket initialized");
    });

    export default class extends Controller {
    static values = { feature: String };

    async connect() {
    await prom;
    bucket.getFeature(this.featureValue).isEnabled;
    }

    async track() {
    await prom;
    bucket.track(this.featureValue);
    }
    }

    // Don't forget in application.js
    // + application.register('bucket', BucketController)
    7 changes: 7 additions & 0 deletions index.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <!-- This will send a check event to Bucket -->
    <div data-controller="bucket" data-bucket-feature-value="myFeature">

    <!-- Clicking this will trigger will track as "trying" the feature "myFeature" -->
    <a href="..." data-action="bucket#track">Try my feature!</a>

    </div>