Skip to content
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

SyncAttributeToApiNamespace Plugin : External API Connection that backfills attributes on API Namespace + API Resources #972

Open
alis-khadka opened this issue Jul 26, 2022 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@alis-khadka
Copy link
Contributor

alis-khadka commented Jul 26, 2022

SyncAttributeToApiNamespace Plugin

For the latest version of this plugin see here:
https://github.com/restarone/violet_rails/blob/master/test/plugin_fixtures/sync_attribute_to_api_namespace.yml

search for SyncAttributeToApiNamespace

    class SyncAttributeToApiNamespace
      def initialize(parameters)  
        @external_api_client = parameters[:external_api_client]
        @api_namespace = @external_api_client.api_namespace
        @api_form = @api_namespace.api_form
      end
      def start
        # Fetching provided inputs (attribute_name, default_value)
        attribute_name = @external_api_client.metadata["ATTRIBUTE_NAME"]
        default_value = @external_api_client.metadata["DEFAULT_VALUE"].nil? ? '' : @external_api_client.metadata["DEFAULT_VALUE"]
        raise 'ATTRIBUTE_NAME is missing!' if attribute_name.nil?
        raise 'The provided attribute is already defined in the ApiNamespace' if @api_namespace.properties.keys.include?(attribute_name)
        # Adding the new-attribute in ApiNamespace
        new_properties = @api_namespace.properties.merge(attribute_name => default_value)
        @api_namespace.has_form = @api_namespace.api_form.present? ? '1' : '0'
        @api_namespace.update!(properties: new_properties)
        # Making the new-attribute non-renderable
        form_properties = @api_form.properties
        form_properties[attribute_name]['renderable'] = '0'
        @api_form.update!(properties: form_properties)
        # Adding the new-attribute to its ApiResources
        @api_namespace.api_resources.find_in_batches(batch_size: 500).each do |resources_batch|
          resources_batch.each do |api_resource|
            # Does not mutate the api-resource if it already has the provided new-attribute
            next if api_resource.properties.keys.include?(attribute_name)
            new_resource_properties = api_resource.properties.merge(attribute_name => default_value)
            api_resource.update!(properties: new_resource_properties)
          end
        end
      end
    end
    # at the end of the file we have to implicitly return the class 
    SyncAttributeToApiNamespace

The following metadata needs to be included for the plugin to work:
image

ATTRIBUTE_NAME: The name of the new attribute that needs to added.
DEFAULT_VALUE: The default value that will be added in the api-namespace and its api-resources properties' value.

Output

image

Note:
The newly added attribute is non-renderable.
We cannot provide files as DEFAULT_VALUE as of now.

@alis-khadka alis-khadka added the documentation Improvements or additions to documentation label Jul 26, 2022
donrestarone pushed a commit that referenced this issue Jul 31, 2022
…API Namespace + API Resources (#970)

Addresses: #870
docs: #972

* feat(): test cases & fixture for SyncAttributeToApiNamespace

* chore(): typo correction

* feat(): migrate SyncAttributeToApiNamespacePlugin test cases to test/plugins folder

* feat(): removing the transaction block

* feat(): making the newly added attribute non-renderable

* chore(): refactoring

* chore(): refactoring
donrestarone added a commit that referenced this issue Aug 1, 2022
…reate External API Connection that backfills attributes on API Namespace + API Resources

# Development changes only, no deploy required ✔️ 

## [Dev workflow] minimal config for nginx in docker + rails


resolves: #976

### Use Nginx in Violet Rails dev! 


<img width="1728" alt="Screen Shot 2022-07-28 at 8 40 00 AM" src="https://user-images.githubusercontent.com/35935196/181507459-bf84bb54-86ad-48d5-8c26-49502565ca14.png">

## [Plugin] Create External API Connection that backfills attributes on API Namespace + API Resources

Addresses: #870
### Documentation: #972
Co-authored-by: Prashant <[email protected]>
donrestarone added a commit that referenced this issue Aug 1, 2022
…reate External API Connection that backfills attributes on API Namespace + API Resources (#998)

# Development changes only, no deploy required ✔️ 

## [Dev workflow] minimal config for nginx in docker + rails


resolves: #976

### Use Nginx in Violet Rails dev! 


<img width="1728" alt="Screen Shot 2022-07-28 at 8 40 00 AM" src="https://user-images.githubusercontent.com/35935196/181507459-bf84bb54-86ad-48d5-8c26-49502565ca14.png">

## [Plugin] Create External API Connection that backfills attributes on API Namespace + API Resources

Addresses: #870
### Documentation: #972
Co-authored-by: Prashant <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant